Site logo

Archived topic

Disable cart link in primary menu

5 replies · Started by Eric on March 15, 2021

Viewing posts 1–6 of 6

I want to use GP's cart icon (and badge showing number of items in cart) in my primary menu, but I want the icon to open a pop-up cart from a plugin rather than the cart page itself. I already know how to make it open the pop-up cart, but the link still contains the href to the cart page and, thus, opens the cart page after opening the pop-up cart. What is the best way to prevent the cart icon from opening the cart page itself?

Hi there,

How are you telling it to open the popup? Javascript?

If so, you can tell your javascript function to ignore the default behavior of the link by using e.preventDefault() at the top of your function. This assumes e is passed as an argument in the click handler.

Thanks, Tom. I'm actually telling it to open the popup using a hook provided by the plugin:

add_filter( 'yith_wacp_open_popup_selectors', function(){ return '.shopping-cart'; } );

I could just apply that filter to a custom menu item, of course, but I wanted to retain the built-in functionality of the GP cart (with its menu bar positioning and item count badge).

You could add a little javascript to disable it yourself:

var cartMenuLinks = document.querySelectorAll( '.main-navigation a.cart-contents' );

for ( var link = 0; link < cartMenuLinks.length; link++ ) {
    cartMenuLinks[ link ].addEventListener( 'click', function( e ) {
        e.preventDefault();
    } );
}

This can be added to a custom JS file, or in the wp_footer hook between <script></script> tags.

Hope it helps!

Thanks, Tom! To get this to work I needed to tweak the first line to this:
var cartMenuLinks = document.querySelectorAll( '.main-navigation a.cart-contents' );

All is good now. :)

Ah, nice catch. Glad it's working! :)

This archived topic is closed to new replies.