11

enter image description here

I'm trying (in archive) handle event after some product was added to cart (1 action on picture), I want catch that moment and update "Total number of products" (3 action on picture) of my mini cart in navigation menu. (With action 2 is all ok)

Not working for me with second code:

$( document.body ).on( 'added_to_cart', function(){
console.log('added_to_cart');
});

My custom code inited after when woocommerce js files are loaded.

If I will edit add-to-cart.min.js core file and insert my own logic, all is working. What problem is?

2
  • You need to be more detailed in your question. And sorry but is a bit hard to understand what you are trying to say.. Please explain your self better. Nov 23, 2017 at 20:06
  • Im trying handle event after some product was added to cart, I want catch that moment and update "Total number of products" of my mini cart in navigation menu.
    – Vitalij
    Nov 23, 2017 at 20:28

1 Answer 1

21

Update (related to your jQuery script)

In Wordpress for jQuery, you need first to use jQuery instead of the alias $ and you should need to specify the "ready" state to allow the DOM to be completely loaded before. I have tested the code below and it works triggering the "added_to_cart" JS event in the browser console, once a product is added to cart:

add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
    if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages
        ?>
            <script type="text/javascript">
                // Ready state
                (function($){ 

                    $( document.body ).on( 'added_to_cart', function(){
                        console.log('EVENT: added_to_cart');
                    });

                })(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
            </script>
        <?php
    endif;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

It display the string "added_to_cart" in the browser console once a product has been added to cart… so it's just working this way as you are expecting.


Original answer:

The mini-cart update/refresh doesn't really need jQuery but custom php function hooked in dedicated woocommerce_add_to_cart_fragments action hook, like in this examples, where the icon count and the content are refreshed each time a product is added to cart.

Refreshing the cart icon count example:

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_mini_cart_refresh_number');
function wc_mini_cart_refresh_number($fragments){
    ob_start();
    ?>
    <div class="mini-cart-count">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </div>
    <?php
        $fragments['.mini-cart-count'] = ob_get_clean();
    return $fragments;
}

Refreshing the mini-cart content example:

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_mini_cart_refresh_items');
function wc_mini_cart_refresh_items($fragments){
    ob_start();
    ?>
    <div class="mini-cart-content" style="display:none;">
        <?php woocommerce_mini_cart(); ?>
    </div>
    <?php
        $fragments['.mini-cart-content'] = ob_get_clean();
        return $fragments;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.


If you need to use other related jQuery "body" delegated events, you can use wc_fragment_refresh or wc_fragments_refreshed too as they are cart related events.

9
  • Thanks for code, but I need only hande jquery based event "added_to_cart"
    – Vitalij
    Nov 23, 2017 at 21:05
  • @Vital your question is not so clear… we don't know how you are setting this jQuery code (hooked, template or registered) and where (if it's on single product pages or in shop archives (or somewhere else (with a product shortcode))… so please try to clarify and give a little more details. Thanks Nov 23, 2017 at 21:14
  • Added picture. All is in shop archive.
    – Vitalij
    Nov 23, 2017 at 21:34
  • @Vital Updated my answer and get it working… please check the details. (The screenshot is so ugly, but explicit) :) … Nov 23, 2017 at 21:56
  • Ok, working now, but.... I don't understand why your code not working if I will place it in js file that goes after add-to-cart.min.js file. Because when your code placed like you writed in functions.php, it goes before add-to-cart.min.js file and it works... :) This is a magic or this event 'added_to_cart' is being overwrited?
    – Vitalij
    Nov 24, 2017 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.