Home » WooCommerce: Exclude Hidden Products from Mini-Cart Counter

WooCommerce: Exclude Hidden Products from Mini-Cart Counter

by Tutor Aspire

When you add a hidden product to Cart, either manually or programmatically, this will be displayed in the Cart, Checkout and Order details pages (I’m not sure why a hidden product behaves like that… but thankfully you can hide hidden products from the Cart/Checkout/Order page with this snippet).

Problem is, even if you hide hidden products from the Cart page, the “Mini-Cart” product counter icon or text (it depends on your theme) will still count them as products (see the screenshot below). So the question is: in conjunction with the snippet aforementioned, how do I exclude hidden products from being counted in the “menu cart” (also called Mini-Cart Widget)?

WooCommerce: exclude hidden products from the Mini-Cart menu widget counter

PHP Snippet: Don’t Count Hidden Products @ WooCommerce Mini-Cart Widget

/**
 * @snippet       Exclude Hidden Products from Cart Count - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=80264
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.4.5
 */ 

// PLEASE NOTE: EMPTY THE CART BEFORE TESTING

add_filter( 'woocommerce_cart_contents_count', 'tutoraspire_exclude_hidden_minicart_counter' );

function tutoraspire_exclude_hidden_minicart_counter( $quantity ) {
  $hidden = 0;
  foreach( WC()->cart->get_cart() as $cart_item ) {
 $product = $cart_item['data'];
 if ( $product->get_catalog_visibility() == 'hidden' ) $hidden += $cart_item['quantity'];
  }
  $quantity -= $hidden;
  return $quantity;
}

You may also like