In woocommerce, we can add a discount to any order using Coupons feature (fixed amount, percent amount…).
Is it possible to add discount amount to any order programmatically where the discount amount can be any amount?
Any help will be appreciated.
The first way is to add a percentage off coupon. To do this, go to WooCommerce > Coupons and click on Add Coupon. Then, enter a coupon code and enter the amount you want to take off in the Discount Amount field. In the Discount Type dropdown, select Percent Off.
Actually you can just make hook before calculate tax etc, get subtotal, apply discount, done :) It automatically apply for Order message etc, it is visible also in backend in proper way. Even after remove hook, info about discount stay in order information.
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_user_discounts');
/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/
function add_user_discounts( WC_Cart $cart ){
//any of your rules
// Calculate the amount to reduce
$discount = $cart->get_subtotal() * 0.5;
$cart->add_fee( 'Test discount 50%', -$discount);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With