Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mircobabini/bdaa38ede4dc3137b0a6580afd7d9d30 to your computer and use it in GitHub Desktop.
Save mircobabini/bdaa38ede4dc3137b0a6580afd7d9d30 to your computer and use it in GitHub Desktop.
<?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