Hooks (action & filter)

Hooks (action & filter)

Hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively throughout WordPress and WooCommerce and are very useful for developers.

Read more about action and filter hooks here:

In Klarna Payments for WooCommerce there aren’t many action and filter hooks available. But there can be some, mainly for modifying the order data sent to Klarna.


How and where to insert the code?


To get the functions exemplified in this section to work you need to add the code to your theme’s functions.php. You can add it as its own plugin or through the Code Snippets plugin, or something similiar of your choice.


Filters


Anonymize product names sent to Klarna

To modify the order data that is sent to Klarna, to anonymize the product names, you can add this filter. You can use the Code Snippets plugin, or something similar of your choice.

add_filter(
	'kp_wc_api_request_args',
	function( $request_args ) {
		foreach ( $request_args['order_lines'] as $index => $order_line ) {
			if ( ! isset( $order_line['type'] ) ) {
				$request_args['order_lines'][ $index ]['name'] = md5( $order_line['name'] );
			}
		}
		return $request_args;
	}
);

This will result in the product names sent to Klarna will be just a random series of letters and numbers:

The real name of the product is still visible in the WooCommerce order. It is only in Klarna’s system where the name is anonymized/censored.


Change the base region for API requests

If you have a European Merchant ID (MID) but also have “Global offering” enabled, Klarna Payments may allow USD as currency.
For Klarna Payments to work the currency must match the country, meaning that you can’t have USD currency when the region is Europe for example.

This filter, where the region can be manually selected, fixes this issue.

/**
 * This filter let you change the base region for API requests. Possible values:
 * - Europe             → '' (leave empty)
 * - North America      → '-na'
 * - Oceania            → '-oc'
 *
 * Default: ''
 */
add_filter(
	'klarna_base_region',
	function ( $region ) {
		return '-na';
	}
);


Change the label on the WooCommerce pay button

If you are using the WooCommerce Checkout block and want to change the label on the Pay button, you can use this change_kp_order_button_label filter.

This is hard coded by default, but by changing the return value (in this example “Pay with Klarna”) you can set a label of your choosing.

<?php
function change_kp_order_button_label( $label ) {
	return 'Pay with Klarna';
}
add_filter('kp_blocks_order_button_label', 'change_kp_order_button_label');

Force locale to a specific country and language

Klarna Payments listens to https://developer.wordpress.org/reference/functions/get_locale/ to set the locale. With this snippet you can force a specific country and language, United States and English in the example below.

Locales should be formatted as a language tag consisting of a two-letter language code combined with a two-letter country code according to RFC 1766. Examples are en-us for US English, en-gb for British English and sv-se for Swedish (in Sweden).

Countries are handled as two-letter country codes according to ISO 3166 alpha-2. Examples are us for the United States, gb for Great Britain and se for Sweden.

You can also find more Klarna specific info in their API reference for data types.

/* Force English, US locale. */
add_filter(
	'kp_locale',
	function( $locale ) {
		return 'en-US';
	}
);

The following values are applicable:

AT: “de-AT”, “de-DE”, “en-DE”
BE: “be-BE”, “nl-BE”, “fr-BE”, “en-BE”
CH: “it-CH”, “de-CH”, “fr-CH”, “en-CH”
DE: “de-DE”, “de-AT”, “en-DE”
DK: “da-DK”, “en-DK”
ES: “es-ES”, “ca-ES”, “en-ES”
FI: “fi-FI”, “sv-FI”, “en-FI”
GB: “en-GB”
IT: “it-IT”, “en-IT”
NL: “nl-NL”, “en-NL”
NO: “nb-NO”, “en-NO”
PL: “pl-PL”, “en-PL”
SE: “sv-SE”, “en-SE”
US: “en-US”.


Linear Checkout for WooCommerce compatibility

With version 3.6.0 of Klarna Payments, we introduced compatibility with Linear Checkout for WooCommerce by Cartimize. While that is part of it, you’ll also need the following code snippet to make it work as expected:

function kp_cartimize_compat () {
  if ( defined( WC_KLARNA_PAYMENTS_PLUGIN_PATH ) ) {
	  include_once WC_KLARNA_PAYMENTS_PLUGIN_PATH . '/templates/klarna-payments-categories.php';
  }
}

add_action(	'cartimize_get_payment_methods_html', 'kp_cartimize_compat' );