Site logo

Archived topic

WooCommerce Show Cart In Primary Menu If Not Empty

10 replies · Started by Fahmi on March 16, 2018

Viewing posts 1–11 of 11

Hi!

I need some help.

How to show Cart in Primary Menu only if the cart not empty?

Having Cart menu item there when it empty not really does anything. Therefore it would be nice to hide it when the cart is empty.

Thank you.

Hi there,

Give this PHP a try:

add_filter( 'generate_woocommerce_menu_item_location', 'tu_hide_empty_cart_icon' );
function tu_hide_empty_cart_icon( $location ) {
    if ( class_exists( 'WooCommerce' ) && sizeof( WC()->cart->get_cart() ) > 0 ) {
        return $location;
    }

    return 'none';
}

Let me know :)

I am not sure why.
But the code would work as intended when I remove class_exists( 'WC' ) &&.

Now with your help, I got it working.

Thank you Tom.

You're welcome!

Hi

The above (also leaving out class_exists( ‘WC’ ) &&) worked for me on desktop, however, the cart is not hiding on mobile when it's empty.

Is there a way to achieve the same function on mobile as well?

TIA

(Generate Press Premium 1.6)

It should work everywhere. Can you link me to your site?

It's a new website (WIP), so it's currently in maintenance mode.

I have created a subscriber login to acces:

How it's showing: https://imgur.com/PEIm3j2
How I'd like it to look (cart disabled in customiser): https://imgur.com/GORYRZD
My PHP code: https://imgur.com/PJBfBiV

I've put the php code in 'wp-head' in GP hooks, I'm not sure if that is what's causing the problem.

Seems like a bug. I'll get it fixed in the next version.

Thank you!

Okay good to hear, thank you :)

Is this already working on mobile? Many thanks :-)

Not yet - this fix will be in 1.6.3 :)

This archived topic is closed to new replies.