Skip to content

Instantly share code, notes, and snippets.

@DanielMarklund
Created November 22, 2024 11:02
Show Gist options
  • Save DanielMarklund/26122428c68c589453ec7673d49a28de to your computer and use it in GitHub Desktop.
Save DanielMarklund/26122428c68c589453ec7673d49a28de to your computer and use it in GitHub Desktop.
Make WooCommerce Klarna Checkout compatible with Aelia + Redlight Media Klarna Shipping
<?php
/**
** What:
* The following pattern should be enough to make the following plugins compatible with each other:
* - Aelia Currency Switcher
* - Aelia Tax Display by Country
* - Klarna Checkout (Krokedil)
* - Klarna Shipping (Redlight Media) + their PostNord & Budbee addons
*
** Why:
* Having all the above plugins won't work well out of the box. Shipping prices will be rendered incorrectly and switching country via the Klarna iframe will make the shipping options render incorrectly.
* This seems to be the only way to make all these plugins work with each other and still retain the possibility to switch countries in the checkout.
*
** Note:
* Please note that you need to place these snippets etc where they belong in your code base. Use this as a guideline.
* You should also implement checks to see if the Aelia plugins are activated or not.
* This is NOT a simple copy and paste – please adjust the different parts to your theme needs and use it as an example.
*
*
* @description Make WooCommerce Klarna Checkout compatible with Aelia + Redlight Media Klarna Shipping
* @author Daniel Leis / Vapes Lidköping AB, with snippets and help from Redlight Media AB and Krokedil AB
* @compatible WooCommerce 7.2.0
*/
// Remove the Aelia currency conversion filter from WooCommerce package rates, or the shipping prices rendered in Klarna will be wrong
$WC_Aelia_CurrencySwitcher = Aelia\WC\CurrencySwitcher\WC_Aelia_CurrencySwitcher::instance();
remove_filter('woocommerce_package_rates', array($WC_Aelia_CurrencySwitcher->currencyprices_manager(), 'woocommerce_package_rates'));
// Switch the country, currency and locale in Klarna based on customer billing country
// This will load the Klarna iframe with the correct settings
add_filter( 'kco_wc_api_request_args', 'change_klarna_country' );
function change_klarna_country( $request_args ) {
// Use the default country from WooCommerce customer settings, Klarna session, or WooCommerce base country
$selected_country = WC()->customer->get_billing_country() ?: WC()->session->get('klarna_country') ?: WC()->countries->get_base_country();
// Since the Aelia "Tax display by country" widget is based on a country selection instead of a currency, we need to check for country and set the currency based on it. Usually it's the other way around. This should instead be setup via ACF or similar and not be hard coded.
$countries_settings = array(
'DK' => array(
'purchase_country' => 'DK',
'billing_countries' => array('DK'),
'shipping_countries' => array('DK'),
'purchase_currency' => 'DKK',
'locale' => 'en'
),
'SE' => array(
'purchase_country' => 'SE',
'billing_countries' => array('SE'),
'shipping_countries' => array('SE'),
'purchase_currency' => 'SEK',
'locale' => 'sv'
),
'NO' => array(
'purchase_country' => 'NO',
'billing_countries' => array('NO'),
'shipping_countries' => array('NO'),
'purchase_currency' => 'NOK',
'locale' => 'nb'
),
'DE' => array(
'purchase_country' => 'DE',
'billing_countries' => array('DE'),
'shipping_countries' => array('DE'),
'purchase_currency' => 'EUR',
'locale' => 'de'
)
);
if (array_key_exists($selected_country, $countries_settings)) {
$settings = $countries_settings[$selected_country];
$request_args['purchase_country'] = $settings['purchase_country'];
$request_args['billing_countries'] = $settings['billing_countries'];
$request_args['shipping_countries'] = $settings['shipping_countries'];
unset($request_args['billing_address']);
unset($request_args['shipping_address']);
$request_args['billing_address']['country'] = $settings['purchase_country'];
$request_args['shipping_address']['country'] = $settings['purchase_country'];
$request_args['purchase_currency'] = $settings['purchase_currency'];
$request_args['locale'] = $settings['locale'];
}
return $request_args;
}
// Example usage of the shortcode for Aelia country selector
echo do_shortcode('[aelia_tdbc_country_selector_widget widget_type="dropdown"]');
// Render Klarna
do_action( 'kco_wc_before_snippet' );
kco_wc_show_snippet();
do_action( 'kco_wc_after_snippet' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment