Wordpress

Klarna – How to auto capture WooCommerce order programmatically

If you’re using Klarna Payments or Klarna Checkout plugin with Klarna Order management plugin within your WooCommerce based webshop and you got some custom order statuses implemented you might want to change the default auto capture order feature on “Completed” order status.

Unfortunately, Klarna Order Management plugin only has an option to select or deselect the auto capturing for “Completed” order status.

In order to set auto capturing to a different order status, either one of the defualt ones or your custom order status, you need to get your hands dirty a bit. It’s not too bad though as you can use the method from the Klarna Order Management plugin, rather than programming the solution from scratch, using the Klarna API.

So here’s the snippet you can use to do that:

if(class_exists( 'WC_Klarna_Order_Management' )){
  $kom = WC_Klarna_Order_Management::get_instance();
  $kom->capture_klarna_order($order_id, true);
 }

So, first we check if the WC_Klarna_Order_Management class exists in order not to break anything. This class will likely be available if the Klarna Order Management plugin is installed. Then we get the instance of the WC_Klarna_Order_Management Singleton class and call the capture_klarna_order method. It’s important to pass the order ID and second parameter as true to it.

That’s pretty much it, when this code is executed, it will send capture call to Klarna.

In order to fire it at the right time, you can use the appropriate WooCommerce hook. In my case, I used the woocommerce_order_status_changed hooked which gave me the necessary $order_id parameter. Then it’s all about checking whether the current order status change is the order status you choose to capture the order.

Full code should look something like this:

if(class_exists('WC_Klarna_Order_Management')){
  add_action('woocommerce_order_status_changed', 'your_callback_fn', 10, 3);
  function your_callback_fn($order_id, $changed_from, $changed_to){
    if($changed_to == 'your-custom-order-status'){
      $kom = WC_Klarna_Order_Management::get_instance();
      $kom->capture_klarna_order($order_id, true);
    }
  }
}

DISCLAIMER: Please use the code snippet above carefully, don’t use it in production directly, try it on the staging setup first and change it to suit your needs. I had to change the code a bit from my implementation and this exact code snippet is not tested, but the principle is the same. If you need help with implementation, please contact us.

2 thoughts on “Klarna – How to auto capture WooCommerce order programmatically”

    1. Hi Mark, I think you can just modify my snippet to check if the order status is changed to either completed or processing, maybe something like this: if($changed_to == ‘completed’ || $changed_to == ‘processing’) but please make sure to test it first on staging setup 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newsletter Signup

Subscribe to our newsletter below and never miss the latest articles.