mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
https://stackoverflow.com/questions/70063577/woocommerce-double-discount-on-sale-products-with-coupon/70066034
30 lines
1.3 KiB
Text
30 lines
1.3 KiB
Text
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {
|
|
// Only apply for this coupon
|
|
if ( $coupon->get_code() == 'doublediscount' ) {
|
|
// On sale
|
|
if ( $cart_item['data']->is_on_sale() ) {
|
|
// Regular price
|
|
$cart_item_regular_price = $cart_item['data']->get_regular_price();
|
|
|
|
// Sale price
|
|
$cart_item_sale_price = $cart_item['data']->get_sale_price();
|
|
|
|
// Calculate the percentage difference
|
|
$cart_item_diff = $cart_item_regular_price - $cart_item_sale_price;
|
|
$cart_item_percentage = round( $cart_item_diff / $cart_item_regular_price * 100, 0 );
|
|
|
|
// Get maximum added discount
|
|
$max_added_discount = $coupon->get_amount();
|
|
|
|
// Less than maximum added discount
|
|
if ( $cart_item_percentage < $max_added_discount ) {
|
|
$discount = round( ( $price_to_discount * $cart_item_percentage ) / 100, 0 );
|
|
}
|
|
} else {
|
|
$discount = 0;
|
|
}
|
|
}
|
|
|
|
return $discount;
|
|
}
|
|
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
|