3

I'm trying to hide out the coupon code field in the cart for a few excluded products. I've added a product category and exclude this category from the coupon use.

A snippet restricts the cart so only one product at a time is allowed. It's unnecessary to show the coupon code for excluded products in this case. The validation would not let the user apply the coupon but it would be even better if they didn't even see the coupon fields.

This is a snippet I found that finds a product category and displays a message:

// Find product category
add_action( 'woocommerce_check_cart_items', 'checking_cart_items', 12 );
function checking_cart_items() {
// set your special category name, slug or ID here:
$special_cat = 'myproductcategory';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $item = $cart_item['data'];
    if ( has_term( $special_cat, 'product_cat', $item->id ) )
        $bool = true;
}
// Displays a message if category is found
if ($bool)
    echo '<div class="checkoutdisc">A custom message displayed.</div>';

}

This is the general snippet that hides the coupon code in the cart:

// hide coupon field on cart page
function hide_coupon_field_on_cart( $enabled ) {

if ( is_cart() ) {
    $enabled = false;
}

return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );

How can I make these functions work together?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Dudikowski
  • 110
  • 7

1 Answers1

5

Update: Compatibility with WooComerce 3+

Yes it's possible, using this combination of code:

add_filter( 'woocommerce_coupons_enabled', 'conditionally_hide_cart_coupon_field' );
function conditionally_hide_cart_coupon_field( $enabled ) {
    // Set your special category name, slug or ID here:
    $special_cat = array('clothing');
    $bool = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $wc_product = $cart_item['data'];
        // Woocommerce compatibility
        $product_id = method_exists( $wc_product, 'get_id' ) ? $wc_product->get_id() : $wc_product->id;

        $main_product_id = $cart_item['variation_id'] > 0 ? $cart_item['product_id'] : $product_id;
        if ( has_term( $special_cat, 'product_cat', $main_product_id ) )
            $bool = true;
    }

    if ( $bool && is_cart() ) {
        $enabled = false;
    }
    return $enabled;
}

Naturally, this goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


References:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • One more thing. How could I hide a div that I show in the cart for the same condition. Some instructions for the coupon that could be hidden in this case. – Dudikowski Sep 05 '16 at 08:54
  • Sorry, missed that. Fixed it now. – Dudikowski Sep 05 '16 at 09:16
  • The cart page starts with some instruction telling users how the coupon code works. I'm using a page building block from my theme. So it's not an element that WooCommerce adds. It's a div that could be hidden with display:none but only if this product category is used. – Dudikowski Sep 05 '16 at 09:22
  • @Dudikowski Look in the chat… you have information about. – LoicTheAztec Sep 06 '16 at 06:11
  • @LoicTheAztec this code prevents me from accessing "wp-admin" it won't let me access, do you know why? – Williams Aug 27 '20 at 16:54