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 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');

Change the status on rejected orders

If Klarna denies the order after a purchase has been made, the plugin will set the order status to Failed. Earlier this was set to On hold. The status that is set can be changed through a filter.

Example for the status filter on rejected purchases when an order has been placed:

/**
 * Set the order status when Klarna rejects a purchase after its been placed and the fraud check has completed.
 * 
 * @param string $status The order status to set when Klarna rejects a purchase. Default is 'failed'.
 
 * @return string
 */
function kp_custom_order_rejected_status( $status ) {
	// Example to set the order status to on-hold when Klarna rejects a purchase. You can find a list of all order statuses that can be used by default in WooCommerce here: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/src/Enums/OrderStatus.php
	// Other statuses added by plugins can also be used, as long as you know the status slug.
	return 'on-hold'; 
}
add_filter( 'kp_order_rejected_status', 'kp_custom_order_rejected_status' );

If the customer doesn’t finalize the purchase after clicking the Place order button and has been directed to the Klarna pop-up to finalize the purchase, there will be an authorization rejection from Klarna. This can, for example, be that the customer closes the window or tries to pay with a card with not enough funds. In this case the order doesn’t get the status Failed, instead it remains as Pending payment.

There is an action that makes it possible to listen to this event and, for instance, set another status on these orders.

Example for the action on authorization rejections from Klarna during the checkout process:

/**
 * Handle the Klarna authorization rejection during the checkout process.
 * 
 * @param WC_Order $order The WooCommerce order object.
 * 
 * @return void
 */
function kp_custom_handle_auth_rejected( $order ) {
	// Set the order to failed instead of having it remain as pending payment when its been rejected by Klarna in the checkout process. This can be any logic that you want to run during this stage using the order from WooCommerce.
	$order->set_status( 'failed', 'Your order note attached to the status transition' );
	$order->save();
}
add_action( 'kp_authorization_rejected', 'kp_custom_handle_auth_rejected' );

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 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' );