Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › Don’t apply other coupon discounts to a BOGO/Free product › Reply To: Don’t apply other coupon discounts to a BOGO/Free product
May 6, 2020 at 7:47 am #15556
anthonyceo
Participant
Thank you. It seems to work.
However, I’m determining what products have free products through custom meta data. In this case, the meta data for free products is called hide_sub.
I don’t want to do it this way. It also won’t work for buy 2 get 1 free, etc. I want to be able to determine programmatically if any product has an auto-applied free product coupon. Is this possible? Thanks.
add_filter('woocommerce_coupon_is_valid_for_product', 'free_product_discount_coupon', 10, 4);
function free_product_discount_coupon($bool, $product, $coupon, $values) {
// Get parent product id for variable products.
// Required to retrieve custom meta data.
$parent_id = $product->get_parent_id();
// Create a parent product from the parent id or
// set $parent_product = $product if the product is the parent product.
if ( $parent_id > 0 ) {
$parent_product = wc_get_product( $parent_id );
} else {
$parent_product = $product;
}
// if coupon is a free product coupon, apply it.
if ($coupon->get_meta('_wjecf_must_select_free_product') === 'yes') {
return $bool;
} else if ($parent_product->get_meta('hide_sub') == 1) {
// Don't allow any other coupons to be applied
// to products with free product coupons (hide_sub = 1).
return false;
}
return $bool;
}