Extra merchant data (EMD)

Extra merchant data (EMD)

Filter order data sent to Klarna for adding EMD (Extra Merchant Data). Add-on functionality for Klarna Checkout for WooCommerce.

This code is not needed for WooCommerce Subscriptions since the function for subscription data is already implemented in Klarna Checkout.

<?php
/**
 * Filter order data sent to Klarna for adding EMD (Extra Merchant Data).
 *
 * Add-on functionality to Klarna Checkout for WooCommerce (https://wordpress.org/plugins/klarna-checkout-for-woocommerce/).
 * This should be used for Klarnas new platform V3.
 *
 * Learn more about available attatchmenat types here: https://developers.klarna.com/en/se/kco-v3/checkout/extra-merchant-data/
 **/

add_filter('kco_wc_api_request_args', 'emd_kco_wc_api_request_args');
function emd_kco_wc_api_request_args( $request_args ) {
	
	if( is_user_logged_in() ) {
		
		// User is logged in - add user_login as unique_account_identifier
		$current_user = wp_get_current_user();
		
		$klarna_emd_info = array(
			'unique_account_identifier'		=> $current_user->user_login,
			'account_registration_date'		=> date( 'Y-m-d\TH:i', strtotime( $current_user->user_registered ) ),
			'account_last_modified'			=> date( 'Y-m-d\TH:i' ),
		);
		$klarna_emd_info = array( $klarna_emd_info );
		
	
	} else {
		
		// User is not logged in - send empty params
		$klarna_emd_info = array(
			'unique_account_identifier'		=> '',
		);
		$klarna_emd_info = array( $klarna_emd_info );
		
	}
    
	$body_attachment = json_encode( array(
			'customer_account_info' => $klarna_emd_info
		) );
			
	$request_args['attachment']['content_type'] = 'application/vnd.klarna.internal.emd-v2+json';
	$request_args['attachment']['body']         = $body_attachment;
	
	return $request_args;
}

Adding the code


There are several ways to add code to your site, a couple of examples are:

Please note that we at Krokedil do not offer support for third party plugins.