Klarna Checkout – 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.
1. Simple CSS changes
2. Remove, add and move with action hooks
3. Create your own template file
templatesfolder in the plugin. To overwrite the file, copy it from the plugin, then paste it into the
/woocommercefolder in your theme. Now you have the opportunity to make the changes you want. Keep in mind that the action hooks
woocommerce_after_checkout_formmust remain for the plugin to work properly.
1. Modify order data sent to Klarna
kco_wc_api_request_argsas described in the following example:
2. Display Klarna Checkout even on free orders
If you have a coupon that brings the total order value down to zero making the order free for the customer, by default this will show the standard WooCommerce checkout instead of the Klarna Checkout. If you would rather want to show the Klarna Checkout for this you can use the filter
kco_check_if_needs_payment. And return false instead of true to this filter.
3. Set a forced purchase country
If you need to set a forced purchase country for any reason other then what the store country is and you don't want to use geolocation, then you can use this snippet to do so. Simply change SE for any other two letter country code that you want to use.
4. Only accept purchases from customer over 18 years of age.
Code example on how to only accept purchases from people over 18 years of age with Klarna Checkout for WooCommerce (plugin for Klarnas v3 platform - https://wordpress.org/plugins/klarna-checkout-for-woocommerce/).
5. Add Cancellation terms sent to Klarna
URL for the cancellation terms page of the merchant. The URL will be displayed in the email that is sent to the customer after the order is captured (max 2000 characters).
6. Add additional checkboxes
There are times when you might want to add an additional checkbox, like a newsletter sign-up or something else.
To show the checkbox with associated text in the Klarna Checkout we need to send the info about this to Klarna along with all other order/cart information. We do this by adding the filter kco_additional_checkboxes in the following way:
Text - In the field text you can add the text that you want to display in connection with this checkbox.
Checked - In the field checked you set whether you want the box checked (true) or unchecked (false) on the initial load of the checkout.
Required - In the field required you can set if this checkbox is required (true) or not (false) to be able to complete the order.
Save the result in the order
When the purchase is completed by the customer and are redirected to the Thank you page we can collect the information, which is whether the customer checked the box or not. We do this with the action hook kco_wc_confirm_klarna_order in the following way:
In this example our checkbox is for letting people subscribe to a newsletter through MailChimp in the way the plugin MailChimp for WooCommerce wants it. Here you need to change the name on the post meta field to the name of where your plugin saves the information. In our example the number 1 is saved in the post meta field mailchimp_woocommerce_is_subscribed if the customer has opted in to subscribe to the newsletter. The process of sending this information to the newsletter provider is something the plugin for the newsletter, in the case MailChimp for WooCommerce, handles. That is both outside of the Klarna plugin and the examples above.
Note that this is only an example on how you save the information with the order and we can't guarantee that we have all the updated info on how a specific plugin other than our own handles things, so you need to look this up for yourself.
How and where to insert the code?
7. Modify payment method icon
An icon is displayed next to the payment method name on the checkout page.
If you want to change this icon there is a filter available for that.
By using the filter
wc_klarna_checkout_icon_html in the following way you can change it to your needs.
A set of different logos/badges to use can be found at the Klarna Developers site.