Created
April 9, 2024 08:03
-
-
Save mircobabini/bdaa38ede4dc3137b0a6580afd7d9d30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Don't apply coupons to the product addons from PPOM for WooCommerce plugin. | |
ì */ | |
add_filter( 'woocommerce_coupon_get_discount_amount', function ( $discount, $discounting_amount, $cart_item, $single, $coupon ) { | |
if ( ! function_exists( 'ppom_get_field_prices' ) || ! function_exists( 'ppom_price_get_addon_total' ) ) { | |
return $discount; | |
} | |
if ( $coupon->is_type( 'percent' ) ) { | |
$ppom_addons_total = 0; | |
if ( isset( $cart_item['ppom']['fields'] ) ) { | |
$product_id = $cart_item['product_id']; | |
$variation_id = $cart_item['variation_id'] ?? ''; | |
$ppom_fields_post = $cart_item['ppom']['fields']; | |
$product_quantity = floatval( $cart_item['quantity'] ); | |
if ( $ppom_fields_post ) { | |
$ppom_field_prices = ppom_get_field_prices( $ppom_fields_post, $product_id, $product_quantity, $variation_id, $cart_item ); | |
$ppom_addons_total = ppom_price_get_addon_total( $ppom_field_prices ); | |
} | |
} | |
$adjusted_amount = $discounting_amount - $ppom_addons_total; | |
if ( $adjusted_amount < 0 ) { | |
$adjusted_amount = 0; | |
} | |
$discount = $adjusted_amount * ( $coupon->get_amount() / 100 ); | |
} | |
return $discount; | |
}, 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment