Home Close mini-cart dropdown automatically after 2 seconds in WooCommerce
Reply: 1

Close mini-cart dropdown automatically after 2 seconds in WooCommerce

itayko Published in 2017-12-07 19:48:08Z

In my woocommerce shop, link, I have changed the add to cart button, when a user click on the add to cart button the cart dropdown is droping from to top, how can i close it after 2 seconds unless touched, (flatsome theme) someone can help me figure out what js or other solution i can add to make the cart dropdown close itself after 2 secods?

If someone wants to add a quantity box with +/- next to the add to cart that will show the cart dropdown here is the codes: enjoy.

// JS
function addToCartLink(evt, pid) {
    var x = jQuery("#quantity_" + pid);
    var y = evt.closest("div");
    var qty = y.getElementsByClassName("input-text")[0].value;

 const addToCartUrl = '/?wc-ajax=add_to_cart';
    var xWWWFormUrlencodedData = "quantity=" + qty;
xWWWFormUrlencodedData += "&product_id=" + pid;
 jQuery.post(addToCartUrl, xWWWFormUrlencodedData, {
        withCredentials: true,
        headers: {
            'Cache-Control': 'no-cache',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Pragma': 'no-cache'
    }).success(result => {
        if (result.error) {
            console.warn('The product has been added to the cart despite that the result object indicates an error!');
        console.log('Success.', result);
        jQuery(".header .cart-icon").replaceWith(result.fragments[".header .cart-icon"]);


 return false;


and in functions.php

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() && ! is_cart() ) {

        $html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
        //$html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';

        $html .= '<div class="quantity buttons_added">';
        $html .= '<input type="button"  value="-"  class="minus button is-form">';
        $html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
        $html .= '<input type="button" value="+" class="plus button is-form">';
        $html .= '</div>';

    return $html;
LoicTheAztec Reply to 2017-12-11 21:55:49Z

You could try something like this (without guaranty as I can't test it):

add_action( 'wp_footer', 'custom_jquery_script' );
function custom_jquery_script(){
            $('body').on( 'added_to_cart', function(){
                setTimeout( function(){
                    $('ul.nav.top-bar-nav > .cart-item').removeClass('current-dropdown');
                }, 2000 );


When an item is added to cart, class current-dropdown is added to:

<li class="cart-item has-icon has-dropdown">

and will apply a CSS to this Child html element to make it visible (the cart popin content):

<ul class="nav-dropdown nav-dropdown-default" style="">

So with my jQuery code, I catch "body" delegated event added_to_cart and I use setTimeout() javascript function to remove that current-dropdown class after 2 seconds (2000 milli seconds). When removing the class, it hides automatically the mini-cart content pop-in…

That should be the right way.

If it doesn't work, you can try (2 alternatives):

1) to replace in the code 'added_to_cart' by 'adding_to_cart' event instead
2) to replace $('body').on( 'added_to_cart', function(){ by:

$('.add_to_cart_button').click( function(){

I hope it will work…

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.379165 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO