Reasons for Hiding the Coupon Field
- Performance: Keep people from using up server resources by repeatedly attempting to guess coupon codes when there aren’t any coupons that apply to a product
- Administration: You don’t want people asking your team how they can obtain a coupon
- Design: You simply want to keep the Cart and Checkout page cleaner for a particular product.
This code snippet will work for a single product. If you want to do this for multiple products, that would require a more complex code snippet or a plugin.
How to Hide the Coupon Field
- Go to the products table within your WordPress dashboard (wp-admin/edit.php?post_type=product)
- Hover over the name of the desired product and note the ID
- Copy the code snippet below and add it to your website.
- Adjust line 5, replacing 12345 with the product ID from step 2
- Adjust line 26, replacing 12345 with the product ID from step 2
PHP
// HIDE COUPON FIELD ON THE CHECKOUT PAGE
function disable_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$product_id = 12345;
$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 = 12345;
$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' );