How to hide WooCommerce's coupon field for a specific product?
P粉138871485
2023-07-28 11:22:20
<p>I'm trying to hide the coupon fields for certain products in WooCommerce cart and checkout page. After searching on Google, I found some code that hides the coupon field, but only for one product. </p><p>How can I handle multiple products in this code: </p><p><br /></p>
<pre class="brush:php;toolbar:false;">// hide coupon field on the checkout page
function disable_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$product_id = 240790;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart === true )
{
$enabled = false;
}
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'disable_coupon_field_on_checkout' );
// hide coupon field on the cart page
function disable_coupon_field_on_cart( $enabled ) {
if ( is_cart() ) {
$product_id = 240790;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart === true )
{
$enabled = false;
}
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'disable_coupon_field_on_cart' );</pre>
The code below will handle multiple product IDs and/or variant IDs for use in the shopping cart and checkout pages, disabling the coupon field for these products.
Should be useful