huykon225
huykon225

Reputation: 581

How to run my custom ajax after woocommerce ajax add_to_cart?

I have created a ajax file handle after click add_to_cart. but my problem is my ajax file always run before woocommerce file (like my picture). How to run my custom ajax after woocommerce ajax add_to_cart ?

enter image description here

Upvotes: 2

Views: 1500

Answers (2)

fd1247
fd1247

Reputation: 25

So I realize this post is old but I am experience the same problem. I used setTimeout to delay the ajax request by 500ms

jQuery(document).ready(function($) {
console.log('ajax-minicart.js loaded');
// What event do we want our AJAX to fire on? ADDING A PRODUCT TO CART.
$('.ajax_add_to_cart').on('click', function () {
    // Fix
    setTimeout( function () {
        console.log('clicked');

        // What data do we need to send? URL TO AJAX, ACTIONS
        $.post(my_ajax_obj.ajax_url, {
            _ajax_nonce: my_ajax_obj.nonce,
            action: 'update_minicart',
        }, function(response) {
            // What do we want to do with response? REFRESH CART
            console.log(response);
            $('.cart-container').html(response);
        });     
    }, 500);
});

});

I'm not sure of the stability or future-proofing of this fix but it works for now. I'd appreciate any information on why this happens myself.

Upvotes: 0

Tristup
Tristup

Reputation: 3663

However its not a good practice, you can try the below code :

$('.add_to_cart_button').on('click',function(){
    /** write your code here **/            
});

Hope this helps

Upvotes: 1

Related Questions