Aller au contenu principal

Shopify : re-ordonner les moyens de paiement

Shopify plus offre un moyen relativement simple permettant de customiser tout le processus du checkout.

Dans un mini tuto nous allons voir comment re-ordonner les moyens de paiement en fonction du pays.

Shopify facilite cette tache en mettant à notre disposition quelques exemples de code sous Ruby. En s'inspirant de cela, voici le code final:
 

# ================================ Customizable Settings ================================
# ================================================================
# Reorder Gateways
#
# The order in which you would like your gateways to display
# ================================================================
DESIRED_GATEWAY_ORDER_CB = [
  "Carte de crédit","PayPal","KBC/CBC","Belfius","Bancontact","PayPal Express Checkout"
]

DESIRED_GATEWAY_ORDER_NOT_CB = [
  "PayPal","Carte de crédit","Bancontact","KBC/CBC", "Belfius","PayPal Express Checkout"
]

COUNTRY_SORT = {
    country_code_match_type: :include,
    country_codes: ["FR"],
}

# ================================ Script Code (do not edit) ================================
# ================================================================
# CountrySelector
#
# Finds whether the supplied country code matches the entered
# string.
# ================================================================
class CountrySelector
  def initialize(match_type, countries)
    @match_type = match_type
    @countries = countries.map { |country| country.upcase.strip }
  end

  def match?(country_code)
    (@match_type == :include) == @countries.include?(country_code.upcase.strip)
  end
end

# ================================ Script Code (do not edit) ================================
# ================================================================
# ReorderGatewaysCampaign
#
# Reorders gateways into the entered order
# ================================================================
class ReorderGatewaysCampaign
  def initialize(desired_order_cb, desired_order_not_cb, country_sort)
    @desired_order_cb = desired_order_cb.map { |item| item.downcase.strip }
    @desired_order_not_cb = desired_order_not_cb.map { |item| item.downcase.strip }
    @country_sort = country_sort
  end

  def run(cart, payment_gateways)
    address = cart.shipping_address
    return if address.nil?
    #
    country_selector = CountrySelector.new(
        @country_sort[:country_code_match_type],
        @country_sort[:country_codes],
      )
    if country_selector.match?(address.country_code)
      payment_gateways.sort_by! { |payment_gateway| @desired_order_cb.index(payment_gateway.name.downcase.strip) || Float::INFINITY }
    else
      payment_gateways.sort_by! { |payment_gateway| @desired_order_not_cb.index(payment_gateway.name.downcase.strip) || Float::INFINITY }
    end
  end
end

# ================================ Script Code (do not edit) ================================
# ================================================================
#
# check country.
# ================================================================

CAMPAIGNS = [
  ReorderGatewaysCampaign.new(DESIRED_GATEWAY_ORDER_CB, DESIRED_GATEWAY_ORDER_NOT_CB, COUNTRY_SORT),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.payment_gateways)
end

Output.payment_gateways = Input.payment_gateways



################################################
##### Hiding payment method by country #####

 
PAYMENT_METHOD = 'Bancontact'
ELIGIBLE_COUNTRY_CODES = ['BE']
 
if Input.cart.shipping_address and ELIGIBLE_COUNTRY_CODES.include?(Input.cart.shipping_address.country_code)
  Output.payment_gateways = Input.payment_gateways
else
  Output.payment_gateways = Input.payment_gateways.delete_if do |payment_gateway|
    payment_gateway.name == PAYMENT_METHOD
  end
end


PAYMENT_METHOD = 'KBC/CBC'
ELIGIBLE_COUNTRY_CODES = ['BE']
 
if Input.cart.shipping_address and ELIGIBLE_COUNTRY_CODES.include?(Input.cart.shipping_address.country_code)
  Output.payment_gateways = Input.payment_gateways
else
  Output.payment_gateways = Input.payment_gateways.delete_if do |payment_gateway|
    payment_gateway.name == PAYMENT_METHOD
  end
end

PAYMENT_METHOD = 'Belfius'
ELIGIBLE_COUNTRY_CODES = ['BE']
 
if Input.cart.shipping_address and ELIGIBLE_COUNTRY_CODES.include?(Input.cart.shipping_address.country_code)
  Output.payment_gateways = Input.payment_gateways
else
  Output.payment_gateways = Input.payment_gateways.delete_if do |payment_gateway|
    payment_gateway.name == PAYMENT_METHOD
  end
end

################################################

Vous devez ajuster "DESIRED_GATEWAY_ORDER_CB",  "DESIRED_GATEWAY_ORDER_NOT_CB" et "COUNTRY_SORT" en fonction de vos besoins.



Ce dode doit etre ajouter dans Script editor :

  1. Depuis votre interface administrateur Shopify, accédez à Applications > Script editor.
  2. Cliquez sur Créer un script.
  3. Cliquez sur Passerelles de paiement.
  4. Sélectionnez Modèle vierge, puis cliquez sur Créer un script.
  5. Dans la section Code source Ruby, supprimez la ligne de code par défaut : Output.cart = Input.cart
  6. Copiez un script de cette page et collez-le dans la section du code source Ruby.
  7. Modifiez la section Paramètres personnalisables du script pour qu'elle fonctionne dans votre boutique.
  8. Testez votre script. Pour plus d'informations, reportez-vous à Test et débogage des scripts Shopify.
  9. Après test :
    • cliquez sur Enregistrer le brouillon pour enregistrer un brouillon non publié du script, ou
    • cliquez sur Enregistrer et publier pour créer et publier le script.





Nous vous conseillons vivement de tester votre code avant de le publier en production :

Stephane K
Il y'a 11 months
Modifié
Il y'a 11 months
Loading ...