Skip to content

Instantly share code, notes, and snippets.

@munts
Created August 25, 2020 16:16
Show Gist options
  • Save munts/73dfb3e70ea0adbf4e94b38843607dd1 to your computer and use it in GitHub Desktop.
Save munts/73dfb3e70ea0adbf4e94b38843607dd1 to your computer and use it in GitHub Desktop.
Make coupons invalid at product level & Set the product discount amount to zero
// Make coupons invalid at product level
add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $product->get_id(), $disabled_products ) )
$valid = false;
return $valid;
}
// Set the product discount amount to zero
add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $cart_item['product_id'], $disabled_products ) )
$discount = 0;
return $discount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment