Actions & filters

Actions & filters

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 for WooCommerce there are a few action and filter hooks available.


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 WordPress project. You can add it as its own plugin, through the Code Snippets plugin, your theme’s functions.php file or something similar of your choice.


Filters


Modify order & cart data sent to Klarna

kp_wc_api_request_args

Used to modify the order data that is sent to Klarna. In this example the product names are anonymized.

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

klarna_base_region

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

kp_blocks_order_button_label

If you are using the WooCommerce Checkout block and want to change the label on the Pay button, you can use the kp_blocks_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

kp_locale

The Klarna plugin uses the WordPress funktion get_locale() to set the locale. With this snippet you can force a specific country and language, American 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”.


Modify the request timeout time

wc_kp_request_timeout

Modify the timeout time for all http requests sent to Klarna (measured in seconds). Default is 10 seconds.

<?php
/**
 * Filter hook wc_kp_request_timeout
 * Modify the timeout time used for http requests sent to Klarna.
 */
add_filter( 'wc_kp_request_timeout', 'custom_wc_kp_request_timeout' );
function custom_wc_kp_request_timeout( $time ) {
    return 20;
}
?>


Actions


Linear Checkout for WooCommerce compatibility

With version 3.6.0 of Klarna for WooCommerce, 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' );