Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce apply coupon for regular price instead of sale price
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
if ($coupon->type == 'percent_product' || $coupon->type == 'percent') {
global $woocommerce;
$cart_total = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$variable_product1= new WC_Product_Variation( $cart_item["variation_id"] );
$cart_total += $variable_product1 ->regular_price * $cart_item['quantity'];
}
$discount = round( ( $cart_total / 100 ) * $coupon->amount, $woocommerce->cart->dp );
return $discount;
}
return $discount;
}
@treeapron

This comment has been minimized.

Copy link

@treeapron treeapron commented Aug 31, 2017

Hi, I applied your fix but this is what I encountered.
I have the product at retail price X on sale for the price Y.
After putting your cod in ( using Code Snippets plugin ) it calculated the discount % based on retail(X) value, but it deducted it from the Sale (y) value. So it end up giving even more of a discount that was intended to. More discount is taken off sale price?
How can I fix this? The discount deduction should be applied to X value not Y.

@yaniv691

This comment has been minimized.

Copy link

@yaniv691 yaniv691 commented Nov 1, 2017

Hi @treeapron, I'm having the same issue. Did you manage to fix it?

@ruurdv

This comment has been minimized.

Copy link

@ruurdv ruurdv commented Mar 29, 2018

Would love to see a solution that works with fixed discount amounts. Any one has an idea?

Also, using this piece of code brakes the 'add to cart'-button? It's wont redirect to the cart after clicking? The page keeps loading..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment