Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
HOWTO: Adding A Discount or Fee to a WooCommerce Order Programmatically
* The following code assumes you have an WC Order object already. We'll be adding a discount to an order.
* This code cannot be added directly anywhere, it is merely an explanation of how this can be done.
* We will add a discount to the order using Fees
/* Assume order is set up already */
//Always save an order first.
//Set up the Item Fee
$fee = new WC_Order_Item_Fee();
//Give the Fee a name e.g. Discount
//I'll be adding a 50 discount, hence multiply by negative one.
$total_fee = 50 * -1;
//Set the Fee
//Add to the Order
//Recalculate the totals. IMPORTANT!
//Save the Order
//We'll complete the order here, but it doesn't have to be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment