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!
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 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
The array of line items will contain various information. Each array item will have the item’s ID as the array
Each item that has been assigned to the array by the key, will have:
refund_total– Total amount to be refunded for that item
refund_tax– Tax to be refunded
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.
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.
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.
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:
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.
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