When you have special deals like Buy One Get One (BOGO) in your store, it’s a good idea to turn off coupons. This ensures customers don’t use extra discounts because they’re already getting a good deal with BOGO. The code snippet helps you to disable the coupon field on both the cart and checkout pages when you’re running specific BOGO promotions.
add_action('wp_loaded', 'ts_bogo_offer'); function ts_bogo_offer() { // Check if WooCommerce is active if (class_exists('WooCommerce') && is_object(WC()->cart)) { // Define the product IDs for comparison $product_gifted_id = 470; // Check if any product is in the cart $product_in_cart = false; foreach (WC()->cart->get_cart() as $cart_item) { if ($cart_item['product_id']) { $product_in_cart = true; break; } } // see if gift id in cart $product_gifted_cart_id = WC()->cart->generate_cart_id($product_gifted_id); $product_gifted_in_cart = WC()->cart->find_product_in_cart($product_gifted_cart_id); // if no product in cart, remove gift; else, add gift if (!$product_in_cart) { if ($product_gifted_in_cart) { WC()->cart->remove_cart_item($product_gifted_in_cart); } } else { if (!$product_gifted_in_cart) { WC()->cart->add_to_cart($product_gifted_id); } } } } function ts_disable_coupon_field($enabled) { if (is_cart() || is_checkout()) { $enabled = false; } return $enabled; } add_filter('woocommerce_coupons_enabled', 'ts_disable_coupon_field');
Output
When a customer adds any product to the cart, a complimentary item is automatically included, and the code will also hide the coupon field to prevent customers from applying additional coupons.
The coupon field is also removed from the checkout page which will help to prevent customers from applying a coupon during the checkout process.
Let’s examine how the cart and checkout pages, with coupon fields, appeared before applying the code
This is how the coupon field appears on the checkout page before implementing the code.
Alternatively, you can also tie BOGO offers to a coupon code and order amount. Here is how you can add a bogo buy one get one offer with a coupon code for orders over 100 in WooCommerce.