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

Even though the name of Klarna appears differently in checkout with a different translation per language, as documented here, Script Editor custom code should reference the name of the payment method configured in the app's payment method extension, which for this integration, is "Klarna".

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

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 = ["Klarna", "Shopify Payments", “PayPal”]
Output.payment_gateways = Input.payment_gateways.sort_by do |payment_gateway|
  desired_order.index(payment_gateway.name) || Float::INFINITY
end
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 == "Klarna"
  end
end
Output.payment_gateways = available_gateways
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 == "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.)

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 == "Klarna"
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 == "Klarna"