Is it possible to hide woocommerce mini cart when empty?

Hello,

Just wondering if anyone has any ideas on how to hide the WooCommerce mini-cart icon conditionally based on the contents of the cart? Essentially trying to hide it if the cart is empty and show it if any item is in the cart.

I’ve tried with conditions of the but couldn’t find anything that works.

Checking here if anyone has any ideas.
Thanks!

Hi @Bez,
Yes I believe you can use the following conditional logic to hide the mini cart if there’s no products in the cart:
image

You can apply this logic to the mini-cart element.

Thanks for the reply, I had tried that and it didn’t work. My understanding is that {woo_cart_quantity} does not return the number of cart contents, it is meant to be used in a custom cart loop to “Render the input field to add/remove the product quantity inside of the cart.” see this page that mentions it

I did figure out a solution though, posting it here for all future viewers. Also if anyone with more knowledge is digging through this in the future, please let me know if there is a more elegant solution.

What ended up working was creating a custom PHP function and using the echo tag to set the display conditions. Followed the information on this academy page about dynamic data, in the “Advanced: Echo” section. Good video there that walks through the process:

Added this to functions.php

function get_total_cart_quantity() {
    // Get the cart object
    $cart = WC()->cart;

    // Return the total quantity of items in the cart
    return $cart->get_cart_contents_count();
}

Then set the Mini Cart’s display condition similar to the last reply but with the echo function Dynamic data → {echo:get_total_cart_quantity} > 0

Also don’t forget that you need to explicitly allow the function name to call up the dynamic data echo tag. See: Filter: bricks/code/echo_function_names – Bricks Academy

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'get_total_cart_quantity',
  ];
} );

Hopefully, this helps the next person trying to do this or something similar!

2 Likes

Nice stuff! Thanks for sharing the solution.

This didn’t work for me. Still trying to figure it out.