【问题标题】:Shopify 'View all' button implementationShopify“查看全部”按钮实现
【发布时间】:2013-06-20 02:16:27
【问题描述】:

Shopify 的每页最多显示 50 个产品。

为了解决这个限制,我制作了一个 jquery 代码 sn-p。该脚本从每个分页链接中获取 url,并执行 ajax 加载 - 将结果添加到主要内容区域。

它对我来说非常有效 - 但对我的朋友却不是。他每次都缺一页。所以我认为这可能是一个异步问题,他的连接比我的慢。所以我重写了几次脚本以更明确。但这仍然对他不起作用。

经过多次故障排除后,似乎如果以管理员身份登录 - 一切正常。如果未登录,则中间页面无法加载。

这是我最近的代码:

{% if template contains 'collection' %}
 <script>

$(document).ready(function() {

$('#viewFewerProducts').hide();


// when viewAllProducts is clicked
$("#viewAllProducts").click( function (e) {
e.preventDefault();
$("#viewAllProducts , #paginationMagic").hide(); // hide pagination buttons
// and clear out collectionThumbs - but add the ViewAllRow back in.
$("#collectionThumbs").empty().html('<div class="row" id="viewAllRow"></div>');

// see how many pagination links there are. Add 1 because of index 0
var numberOfPages = $('#paginateNumbers .item').length + 1
var path = window.location.pathname;
var pageURL;


// this bit adapted from blog post... but cant remember url
for (var i=1;i<numberOfPages;i++) { 
    $.ajax(
    {
        type: 'GET',
        url:  pageURL = path + "?page=" + i, // build pagination page url
        ajaxI: i, // Capture the current value of 'i'.
        success: function(data)
    {
        i = this.ajaxI; // Reinstate the correct value for 'i'.
    $(data).find('#collectionThumbs').each(function()
    {
      // Read data... and stick it in the page
     var importedCollection = $(data).find("#collectionThumbs a").unwrap();
     importedCollection.appendTo("#viewAllRow");
    });
    },
        error: function(data)
    {
    // Handle errors here.
    }
    });
}
///

    $("#viewFewerProducts").show();

});

// reload the window
$("#viewFewerProducts").click( function (e) {
    e.preventDefault();
    $("#viewFewerProducts").text('loading...').removeAttr('id');
    location.reload();
});


});

</script>
{% endif %}

我已经用其他几种不同的方式编写了它。如果没有登录它就不起作用?我已经检查过了 - 控制台也没有出现任何错误。

所以我的问题是 - 有谁知道为什么登录后它会起作用,但如果没有登录到管理员则不会?它真的很奇怪 - 因为它没有在任何管理页面上运行。

编辑:

{% if template contains 'collection' %}
 <script>

$(document).ready(function() {

$('#viewFewerProducts').hide();



$("#viewAllProducts").click( function (e) {
e.preventDefault();
    $("#viewAllProducts , #paginationMagic").hide();


    var numberOfPages = $('#paginateNumbers .item').length + 1
    var path = window.location.pathname;
    // console.log(path);
    var pageURL;

    //

    for (var i=1;i<numberOfPages;i++) {
    // console.log(i + 'a')
    $.ajax(
    {
    type: 'GET',
    url:  pageURL = path + "?page=" + i,
    beforeSend: function() {
    $("#collectionThumbs").empty();
    },
    ajaxI: i, // Capture the current value of 'i'.
    success: function(data)
    {
    i = this.ajaxI; // Reinstate the correct value for 'i'.

        $(data).find('#collectionThumbs').each(function() {
        // Read data from XML...
         $('<div class="row" id="viewAllRow' + i + '"></div>').appendTo("#collectionThumbs");
         var importedCollection = $(data).find("#collectionThumbs a").unwrap(); 
          importedCollection.appendTo("#viewAllRow" + i );

        // alert("now showing " +  ($("#viewAllRow" + i + " a").length)  + " products" );
        });

        var numberOfRows = $('#collectionThumbs .row').length + 1
        var viewAllRowItem = []

        for (var x=1;x<numberOfRows;x++) {
            //put each row into a variable
             viewAllRowItem[x] = $("#viewAllRow" + x ).clone();
             $("#viewAllRow" + x ).remove();
             // console.log(viewAllRowItem[x])
        }

        for (var x=1;x<numberOfRows;x++) {
                $(viewAllRowItem[x]).appendTo('#collectionThumbs');
        }

    },
    dataType: "html",
    error: function(data)
    {
    // Handle errors here.
    }
    });




}



$("#viewFewerProducts").show();


});


$("#viewFewerProducts").click( function (e) {
e.preventDefault();
$("#viewFewerProducts").text('loading...').removeAttr('id');
location.reload();

});

});

</script>
{% endif %}

上面的代码似乎有效——不知道为什么……是一个消除过程。我确实必须添加一些代码来重新排序加载后的元素(因为一些 ajax 响应比其他响应更快地返回 - 并且以错误的顺序出现在页面上)。

【问题讨论】:

  • 既然您可以使用 Liquid 对任何集合进行分页,那么您为什么要采用这种方法而不是仅仅分页呢?如果您想在每页展示 100 个产品,并且您有 1000 个产品,那么您将获得 10 个页面可供使用,而无需处理 XHR 废话。
  • 嗨,David,我使用 Liquid 进行分页 - ajax 调用会点击这些分页链接以了解从哪个页面提取 html...Shopify 是否已取消其 api 限制?
  • 已进行编辑/更新。如何标记它已解决?
  • 您是否检查过您是否通过不同的登录方式从 Ajax 请求中获取数据?

标签: javascript jquery shopify


【解决方案1】:

$('[js-load-more]').on('click', function(){
  var $this = $(this),totalPages = parseInt($('[data-total-pages]').val()),currentPage = parseInt($('[data-current-page]').val());
  $this.attr('disabled', true);
  $this.find('[load-more-text]').addClass('hide');
  $this.find('[loader]').removeClass('hide');
  currentPage = currentPage+1;
  var nextUrl = $('[data-next-url]').val().replace(/page=[0-9]+/,'page='+currentPage);
  $('[data-current-page]').val(currentPage);
  $.ajax({
    url: nextUrl,
    type: 'GET',
    dataType: 'html',
    success: function(responseHTML){
      $('.grid--view-items').append($(responseHTML).find('.grid--view-items').html());
    },
    complete: function() {
      if(currentPage >= totalPages) {
        $this.remove();
      }
      else {
        $this.attr('disabled', false);
        $this.find('[load-more-text]').removeClass('hide');
        $this.find('[loader]').addClass('hide');
      }
    }
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
        <input type="hidden" value="{{ paginate.next.url }}" data-next-url>
        <input type="hidden" value="{{ paginate.pages }}" data-total-pages>
        <input type="hidden" value="{{ paginate.current_page  }}" data-current-page>
        <div class="load-more_wrap">
          <button class="btn" js-load-more>
            <span load-more-text>Load more</span>
            <span class="hide" loader></span>
          </button>
        </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2014-05-17
    相关资源
    最近更新 更多