This is my first post, so please be gentle. I'm already successfully executing some jQuery codes in my woocommerce cart-page after updating the item quantity (Ajax) with the following code:

$( document.body ).on( 'updated_cart_totals', function(){
    //re-do your jquery

I also need a similar function to execute a jQuery code after adding a cross selling item (with click on the ajax_add_to_cart_button) in the cart-page. So far i checked the cart.js file to find a custom event to handle this - but i was not able to find a solution. Any idea how to execute a jQuery code after successully adding a cross selling item to the cart in the cart-page?

  • Could I get a comment explaining the downvote? That was almost instant...
    – evavienna
    Dec 1 '17 at 0:54
  • Presumably the downvote was for asking a WooCommerce-specific question in the WordPress stack. Dec 2 '17 at 3:04
  • @helgatheviking - and where should i ask this kind of questions?- thx
    – evavienna
    Dec 3 '17 at 14:47
  • Probably where you first commented... ie: stackoverflow.com with the woocommerce tag. My understanding is that this site is for WordPress core-related issues. Dec 4 '17 at 2:30
  • @helgatheviking - ok i understand, makes sence. thank you so much. best
    – evavienna
    Dec 4 '17 at 14:06

From a cursory overview, the cross-sells are added via the same ajax add to cart script as regular shop loop archive products and I don't see a way to distinguish between when it's a cross-sell and when it's not. You could run a script conditionally only on the cart page, and then listen to the added_to_cart trigger, which might then only be happening for cross sells (assuming you don't have other ajax add to cart buttons on the cart page)

Or it appears that the add to cart button is passed as a variable to the added_to_cart trigger. You may be able to do some DOM manipulation there to see if it was part of the cross sells div. Totally wild guess:

$( document.body ).on( 'added_to_cart', function( event, fragments, cart_hash, $button ){
    if( $button.closest( 'div.cross-sells' ) ) {
        // was a cross-sell added to the cart
  • Thank you helga - i tried a couple of things and also your idea to solve my problem - unfortunately i was not able to make it work... maybe another user can help me. best
    – evavienna
    Dec 3 '17 at 14:46
  • Too bad that didn't work. It was just a guess. As I said, it doesn't seem like there's an obvious way to tell them apart. Dec 4 '17 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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