We’ve already seen how to exclude hidden products from the WooCommerce Mini-Cart widget counter; today I want to expand on the same concept and try to recalculate / alter such counter based on custom criteria.
For example, some business models require to count the number of distinct items in the Cart, no matter their respective cart quantities. So, if there are 2x “Item 1” and 4x “Item 2”, this altered counter would show 1+1=2 and not 2+4=6. So, let’s see how this is done – enjoy!

PHP Snippet: Custom Cart Item Count @ WooCommerce Mini Cart Header Widget
/**
* @snippet Alter Cart Counter @ WooCommerce Cart Widget
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 4.6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_cart_contents_count', 'bbloomer_alter_cart_contents_count', 9999, 1 );
function bbloomer_alter_cart_contents_count( $count ) {
$count = count( WC()->cart->get_cart() );
return $count;
}
Hi, the snipet show this error:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘almapet_alter_cart_contents_count’ not found or
invalid function name …
Sorry, ortografic error
Good to know!