Search code examples
javascriptjqueryhtmlajaxjquery-ias

How do I bind and unbind infinite-ajax-scroll so other jQuery works?


Note: I know this is a common issue and a lot has been written about this but I can't seem to fix it.

My Shopify store uses Ajax call's to add products to the cart and jQuery to update the front-end. I recently installed infinite-ajax-scroll but this brought some issues.

When scrolling down to the products loaded by infinite-ajax-scroll, and then click on the add to cart button, the ajax calls & jQuery updates don't work anymore, it redirects me to the cart page.

Here's the inline script I'm using to activate IAS:

<script>
  var ias = jQuery.ias({
    container:  '#products',
    item:       '.single-product',
    pagination: '.pagination-custom',
    next:       '.next'
  });

  ias.extension(new IASSpinnerExtension({
    src: '{{ "spiffygif_36x36.gif" | asset_url }}'
  }));
</script>

This is the code (ajaxify.js line 161 - 194) that's responsible for adding the product to the cart:

Shopify.addItemFromForm = function(form, callback, errorCallback) {
  // Unbind IAS
  ias.destroy();

  var params = {
    type: 'POST',
    url: '/cart/add.js',
    data: jQuery(form).serialize(),
    dataType: 'json',
    success: function(line_item) {
      if ((typeof callback) === 'function') {
        callback(line_item, form);
      }
      else {
        Shopify.onItemAdded(line_item, form);
      }
    },
    error: function(XMLHttpRequest, textStatus) {
      if ((typeof errorCallback) === 'function') {
        errorCallback(XMLHttpRequest, textStatus);
      }
      else {
        Shopify.onError(XMLHttpRequest, textStatus);
      }
    }
  };
  jQuery.ajax(params);

  var variant_id = params.data.split('=')[1]
  $( "#var-id-" + variant_id + " " + "#in-cart-indicator" ).removeClass( "not-in-cart" ).addClass( "triangle-top-right" );

  // Bind IAS
  ias.bind();
};

I've added the bind and unbind method based on this SO post, but without success.

What am I doing wrong?

You can take a look at the page in question here.


Solution

  • I solved this by reinitialising ajaxifyShopify every time the new products finished rendering using infinite-ajax-scroll's rendered event.

    Copy and paste the following code in collection.liquid:

    <script>
    var ias = jQuery.ias({
      container:  '#products',
      item:       '.single-product',
      pagination: '.pagination-custom',
      next:       '.next'
    });
    
    ias.on('rendered', function() { 
      jQuery(function($) {
        ajaxifyShopify.init({
          method: '{{ settings.ajax_cart_method }}',
          wrapperClass: 'wrapper',
          formSelector: '#addToCartForm',
          addToCartSelector: '#addToCart',
          cartCountSelector: '#cartCount',
          toggleCartButton: '.cart-toggle',
          useCartTemplate: true,
          btnClass: 'btn',
          moneyFormat: {{ shop.money_format | json }},
          disableAjaxCart: false,
          enableQtySelectors: true
        });
      });
    })
    </script>