0

I didn't find any result for my issue.

I have Prestashop 1.5.6, I need to execute my jQuery function after blockcart adds the product.

BlockCart and my code starts with the same event, As I far know the unbind disables the other handlers event, how can I restart the unbind in the footer code?.

So my code don't work because Blockcart starts on first

//---------BlockCart Code---------

    overrideButtonsInThePage : function(){
    //for every 'add' buttons...
    $('.ajax_add_to_cart_button').unbind('click').click(function(){
        var idProduct =  $(this).attr('rel').replace('nofollow', '').replace('ajax_id_product_', '');
        if ($(this).attr('disabled') != 'disabled')
            ajaxCart.add(idProduct, null, false, this);
        return false;
    });
    //for product page 'add' button...
    $('#add_to_cart input').unbind('click').click(function(){
        ajaxCart.add( $('#product_page_product_id').val(), $('#idCombination').val(), true, null, $('#quantity_wanted').val(), null);
        return false;
    });


//-------- Footer code--------
$('.button.ajax_add_to_cart_button.exclusive, .button.ajax_add_to_cart_button.btn.btn-default').on('click',function(){
    var id_product = $(this).attr('data-id-product');
    myfunction(id_product);
});

It's possible to detect when blockcart ends the script without a editing the blockCart module with a callback function?

Thanks!

JaviMetal
  • 85
  • 6
  • Check the documentation for any events that blockcart creates. Unless it gives you anything to attach to theres not a ton you can do. Could you elaborate on your code to show me where you make this blockcart call. – Shan Robertson Nov 29 '14 at 00:35
  • if the function you're waiting for is asynchronous (e.g. makes an ajax call) and does not provide a promise or callback, then there is no straightforward way to know when it is done without editing it to provide such guidance. If it is synchronous (no networking operations), then whenever it returns, it is done. – jfriend00 Nov 29 '14 at 00:50

3 Answers3

1

Finally the result was an override the blockCart function.

I did a copy of the overrideButtonsInThePage into my js to override it and I add my code inside the events.

Works perfectly

JaviMetal
  • 85
  • 6
0

Depending on how blockcart functions, you can try:

//-------- Footer code--------
$('.button.ajax_add_to_cart_button.exclusive, .button.ajax_add_to_cart_button.btn.btn-default').click(function(){
    var id_product = $(this).attr('data-id-product');
    setTimeout(function() { myfunction(id_product); }, 0);
});

This will cause your function to execute after the existing stack finishes executing. You can learn more about what this does in Why is setTimeout(fn, 0) sometimes useful?. This will only be helpful if the blockcart handler is synchronous.

Community
  • 1
  • 1
rdubya
  • 2,916
  • 1
  • 16
  • 20
0

You can actually monitor ajax calls by using $.ajaxComplete(); and .ajaxStart()

http://api.jquery.com/ajaxcomplete/

What you can do is compare url the ajax is being sent to. Add to cart URL has GET parameters pre-encoded into it, so distinguishing it from other ajax calls isnt hard. Then on $.ajaxComplete(); you should compare the response result (determin that it was a succes) and then execute your function. This way you wont modify anything, you will just be observing the ajax actions

gskema
  • 3,141
  • 2
  • 20
  • 39