Klarna Docs - Shopify Plus: custom scripts

Shopify Plus: custom scripts

Shopify Plus merchants can use custom scripts to customize their payment methods(s) presentation in Shopify checkout, based on available cart data, as documented below.  Please note: Klarna is not responsible for this sample code; merchants should review & test this code in their store.  

Go to the Script Editor app and follow the instructions per: 

https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/shopify-scripts#cart

Note: each store should customize the bolded parts of the code below to use the appropriate data for the store.

Reorder Shopify payment methods 

Copied, and possibly updated to match the payment method/gateway names for the store, per Shopify’s example code for the “Reorder payment gateways” in the Script Editor app:

RUBY
desired_order = ["Buy now, pay later with Klarna", "Shopify Payments", “PayPal”]
Output.payment_gateways = Input.payment_gateways.sort_by do |payment_gateway|
  desired_order.index(payment_gateway.name) || Float::INFINITY
end

Hide Klarna payment methods based on Cart Presentment Currency

RUBY
cart = Input.cart
available_gateways = Input.payment_gateways
if cart.presentment_currency != "USD"
  available_gateways = available_gateways.delete_if do |payment_gateway|
    payment_gateway.name == "Buy now, pay later with Klarna"
  end
end
Output.payment_gateways = available_gateways

Hide Klarna payment methods based on Shipping Address

RUBY
cart = Input.cart
available_gateways = Input.payment_gateways
if (cart.shipping_address)
  if cart.shipping_address.country_code != "US"
    available_gateways = available_gateways.delete_if do |payment_gateway|
      payment_gateway.name == "Buy now, pay later with Klarna"
    end
  end
end
Output.payment_gateways = available_gateways

(Note: the Shopify API does not provide billing address cart data as parameters for payment gateway custom scripts since the billing address is entered by the customer on the same page where the payment gateway custom script is run.)

Hide Klarna payment methods based on Product SKUs

RUBY
available_gateways = Input.payment_gateways
cart = Input.cart
 
SKUS_TO_HIDE = ["SKU-1234", "..."]
 
cart.line_items.each do |item|
  item.variant.skus.each do |sku|
    if SKUS_TO_HIDE.include? sku
      available_gateways = available_gateways.delete_if do |payment_gateway|
        payment_gateway.name == "Buy now, pay later with Klarna"

Hide Klarna payment methods based on Product Tags

RUBY
available_gateways = Input.payment_gateways
cart = Input.cart
  
TAGS_TO_HIDE = ["hide-klarna", "..."]
  
cart.line_items.each do |item|
  item.variant.product.tags.each do |tag|
    if TAGS_TO_HIDE.include? tag
      available_gateways = available_gateways.delete_if do |payment_gateway|
        payment_gateway.name == "Buy now, pay later with Klarna"