Skip to main content
Shopify: reorder payment methods

Shopify Plus offers a relatively simple way to customize the entire checkout process.

In this mini tutorial, we will see how to reorder payment methods based on country.

Shopify makes this task easier by providing us with  Ruby  some example code. Based on this, here is the final code:
 

# ================================ 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

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

You must adjust "DESIRED_GATEWAY_ORDER_CB",  "DESIRED_GATEWAY_ORDER_NOT_CB" and  "COUNTRY_SORT" according to your needs.


This code must be added in Script editor :

  1. From your Shopify admin interface, go to App > Script editor.
  2. Click Create Script.
  3. Click Payment Gateways.
  4. Select Blank template, then click Create script.
  5. In the Ruby source code section, delete the default line of code: Output.cart = Input.cart
  6. Copy a script from this page and paste it into the Ruby source code section.
  7. Modify the Customizable settings section of the script so that it works in your store.
  8. Test your script. For more information, refer to Testing and debugging Shopify scripts.
  9. After testing:
    • click Save draft to save an unpublished draft of the script, or
    • click Save and publish to create and publish the script.





We strongly recommend that you test your code before publishing it in production:

Profile picture for user admin Stephane K

Écrit le

Il y'a 1 year
Modifié
Il y'a 2 weeks
Loading ...
WhatsApp
Support Habeuk : +237 694 900 622
WhatsApp Send