Important

Okay
  Public Ticket #1364360
add to cart ajax is not working after press load more button
Closed

Comments

  •  2
    Raluca started the conversation

    Hi. I found a bug. The ajax add to cart is not working properly,  only for the first items that are loaded (the first page in this case). When I press the button load more, new products are coming (so this is now the page 2), but for these products the add to cart with ajax is not working, it refreshes the page instead and after that the product is added to cart. 

    Now the link is changed, and now  I can  add again with ajax products on cart for page 2. So the link is changed  it is, for example , loremipsum.com/categoryname?p=2&_=1510315834548 something like that. 

    If I press now the load more button, it should be page 3, but again the link doesn't change, it's the one with ?p=2 and if I add a product to cart, from the ones that belong to page 3, ajax add to cart not working, the page is loading again and the link change with ?p=3. And so on

     If I don't add anything in the cart and only press the load button over and over, the link remains without the pagination something like:  loremipsum.com/categoryname

    So I think the problem is because the link doesn't have the page that was loaded, and only after the refresh it's added and the ajax add to cart is working for the products that belong to the page in the link.

    Can you help me with this issue?

    Thanks

  •  1,315
    Land of replied

    Hello Raluca,

    Have a nice day!

    You should add custom js code to init ajax add to cart when the module ajax load completely.

    Try follow the solution to init add to cart js after ajax call:

    https://magento.stackexchange.com/questions/122458/need-to-initialization-js-after-ajax-response-magento-2

    https://magento.stackexchange.com/questions/92502/magento2-script-type-text-x-magento-init-after-ajax

    To apply the custom js code you should edit the file "/app/code/Lof/AjaxScroll/view/frontend/templates/init.phtml"

  •  2
    Raluca replied

    Hello, 

    I tried to put this code 

    htmlObject=jQuery("#custom_search_autocomplete").html(result);  

    htmlObject.find('[data-role=autocompletesearch-tocart-form], .form.map.checkout').attr('data-mage-init', JSON.stringify({'catalogAddToCart': {}}));             
    htmlObject.trigger('contentUpdated');

    This was the accepted answer from the link you shared. But it's not working. Can you help me out? Is this the code that I should add? And where exactly on init.phtml should I put it?

    Thanks

  •  1,315
    Land of replied

    Hello Raluca,

    Have a nice day!

    Please find the code in the init.phtml file as this:

    window.ias.on('render', function(items){
        SgyIAS._log({eventName: 'render', items: items});
        if (typeof($(items).find('.lof-lazy').lazy) === 'function') {
           $(items).find('.lof-lazy').lazy({
             bind: "event",
            delay: 0
          });
       }
    }); 

    Then you can add your code at here:

    window.ias.on('render', function(items){
        SgyIAS._log({eventName: 'render', items: items});
        if (typeof($(items).find('.lof-lazy').lazy) === 'function') {
           $(items).find('.lof-lazy').lazy({
             bind: "event",
            delay: 0
          });

         //put your custom js code at here
       }
    }); 

  •  1
    Anandhu Vijayan replied

    I stuck with same problem.

    Add to cart button only works with the initial(First) page.

    when hit load more new product loaded.

    when click on add to cart button it refresh the page, and loads second page.

    but add to for first product works fine.

    Hope you can HELP ME.

  •  1,315
    Land of replied

    Hello Anandhu,

    Have a nice day!

    Please try to check the previous post to put the add to cart init function after the ajax scroll load completely.

  • Davi Backendorf replied

    Ves_ProductList too hass this problem on "app/code/Ves/Productlist/view/frontend/web/js/productlist.js" file. It would be interesting to apply this fix too.