I Doubled my Income in a Year

WordPress allowed me to get a better job & better projects in just a Year. I will give you tips, tricks and guide you into WordPress development so you can get more income. Sign up to my 7-day email course!

How to Create WooCommerce Refunds Programmatically

WooCommerce offers a refund option right from the order edit screen. But what if you need to process WooCommerce refunds automatically through code? In this tutorial, we will create the code that will be able to process refunds for a single order.

WooCommerce Refunds

WooCommerce refunds can be created using their own API function wc_create_refund. Let’s first understand what we can pass to that function.

There are 7 arguments that we can pass in an array to that function:

  • amount – The amount to be refunded
  • reason – Reason of the refund
  • order_id – ID of the order we want to refund
  • refund_id – ID of the refund we want to use again and retry
  • line_items – Array of line items which we want to refund from the order
  • refund_payment – Boolean. If true, the refund process will also try to refund the payment through the payment gateway
  • restock_items – If true, it will restock the items back by the quantity of each line item that we have refunded

Line Items

The array of line items will contain various information. Each array item will have the item’s ID as the array key.

Each item that has been assigned to the array by the key, will have:

  • qty – Quantity
  • refund_total – Total amount to be refunded for that item
  • refund_tax – Tax to be refunded

Function to Process WooCommerce Refunds

We will now start creating our own function. This function will receive the Order ID and the refund reason.

In this function we are getting the order by using the function wc_get_order. Then we are checking if the order is of type WC_Order. If it’s not, then we have received a different type of WooCommerce Order which can be something like WC_Order_Refund. We don’t want that.

We also don’t want to refund an order that has already been refunded, so we check for its status. If everything is correct, then we are getting the order items for which we will process the refund. We also define the variable to hold our refund amount and one for the line items.

Getting the Line Items

We will check if we have any order items, and if we do, we will save those line items.

For each line item, we are getting the tax data to calculate the tax refund. We also calculate the refund amount for that line item and sum it up with the current refund amount.

After that, we add all that information to the array of line items.

Creating the Refund

We have everything we need now and we just need to create the refund.

We are passing all our information into an array, with the arguments mentioned at the beginning of this tutorial.

Conclusion

By using the WooCommerce API we can do a lot of stuff automatically. In this tutorial, we got introduced with the WooCommerce refunds and how to create them using the WooCommerce API.

Have you ever had to process refunds? Would you like to process them automatically on some events such as failed products?

If you are a developer who wants to learn more about WooCommerce, check out my eBook:

Book WooCommerce for Developer to buy

About the Author Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

follow me on:

EDD for Developers

In less than 2 weeks, I have earned $1,800 by working on a custom solution with Easy Digital Downloads without any prior experience. Let me teach you how you can too with various custom solutions.

Get the eBook

Leave a Comment:

2 comments
Labee Singh says July 28, 2017

Hi

Does Woo commerce provide any hook or filter when refund is done through admin panel ?

Reply
    Igor Benic says August 2, 2017

    You can only hook into 'wp_ajax_woocommerce_refund_line_items' since that one is done when processing refund through admin panel. But that is not something that would provide you with refund information. You could maybe try and create a hook (inside the previous one) such as a simple do_action('processing_refund_admin_panel') before the refund is processed. After that, you can hook into the wc_create_refund actions (check which exist there) and when check in your function if( did_action('processing_refund_admin_panel') ){}. You could maybe be sure that this refund is processed through the admin panel. I haven’t tried this yet, but you can try and see if it works.

    Reply
Add Your Reply

How to Create WooCommerce Refunds Programmatically

by Igor Benic time to read: 2 min
2
Complete course to Become a WordPress DeveloperJoin the Course
+