Woocommerce - add_to_cart trigger

I am trying to use the add_to_cart WooCommerce trigger to trigger a popup when certain products are added to the cart. So far I have managed to achieve the following results:

jQuery('body').on('added_to_cart',function() {
        alert("testing!");
    });

A warning window is displayed here when a product is added to the cart. However, I would like the warning to be displayed only for certain categories. But how can I check which category the product added to the basket belongs to?

Source to add to cart: https://github.com/woothemes/woocommerce/blob/master/assets/js/frontend/add-to-cart.js

And this trigger in this question:

$( document.body ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
+4
2

, , .

, .ready().

:

jQuery(document).ready(function($){
    $('body').on( 'added_to_cart', function(){
        alert("testing!");
    });
});
+4

, html WooCommerce, .

, Chrome Storefront, : http://demo2.woothemes.com/storefront/shop/. , "", <li> .

$ = jQuery;
var lastCategory;

$('body').on('added_to_cart',function() {
    if(lastCategory === 'radios'){
        alert('a radio was added!');
    }
});

$('.add_to_cart_button').on('click',function() {
    lastCategory = $(this).closest('li').attr('class').split('product-cat-')[1].split(' ')[0];
});
+2

Source: https://habr.com/ru/post/1620548/


All Articles