require(['jquery'], function($) { let loading = false; let page = parseInt($('#product-card-grid').data('current-page')) || 1; let isLastPage = $('#product-card-grid').data('is-last-page') == "1"; function loadMoreProducts() { if (loading || isLastPage) return; loading = true; page += 1; $.ajax({ url: window.location.href.split('?')[0], data: { page: page }, type: 'GET', success: function(res) { const $newCards = $(res).find('#product-card-grid .product-card').filter(function(){ let productId = $(this).data('product-id'); return $('#product-card-grid .product-card[data-product-id="' + productId + '"]').length === 0; }); const $resGrid = $(res).find('#product-card-grid'); const isFinalPage = $resGrid.data('is-last-page') == "1"; if ($newCards.length) { $('#product-card-grid').append($newCards); isLastPage = isFinalPage; loading = false; } else { isLastPage = true; } } }); } $(window).on('scroll', function() { if (!loading && !isLastPage && $(window).scrollTop() + $(window).height() >= $('#load-more-trigger').offset().top - 100) { loadMoreProducts(); } }); });