【问题标题】:Sort Products by Published date in collection page - SHOPIFY按集合页面中的发布日期对产品进行排序 - SHOPIFY
【发布时间】:2018-02-21 12:49:49
【问题描述】:

我们希望我们的产品按发布日期排序(而不是按创建日期)

我确信这在 Shopify 中是可能的,我只是不明白我所缺少的。

  {% assign date_now_sort = 'now' | date:'%s' %}
  {% assign product_sort = collection.products  | sort: 'published_at' | reverse %}
  {% for product in product_sort %}
  {% assign date_published = product.published_at | date:'%s' %}
  {% assign date_now = 'now' | date:'%s' %}
  {% assign date_difference = date_now | minus: date_published %}
  {% if date_difference < 5259492 %}
  <div id="product-loop">
    {% for product in collection.products %}
    {% assign products-per-row = section.settings.products-per-row %}
    <div id="product-listing-{{ product.id }}" class="product-index {% if products-per-row == 6 or products-per-row == "6" %}desktop-2{% cycle ' first', '', '', '', '', ' last' %}{% elsif products-per-row == 4 or products-per-row == "4" %}desktop-3{% cycle ' first', '', '', ' last' %}{% elsif products-per-row == 3 or products-per-row == "3" %}desktop-4{% cycle ' first', '', ' last' %}{% elsif products-per-row == 5 or products-per-row == "5" %}desktop-fifth{% cycle ' first', '', '', '', ' last' %}{% elsif products-per-row == 2 or products-per-row == "2" %}desktop-6{% cycle ' first', ' last' %}{% endif %} tablet-half mobile-half" data-alpha="{{ product.title }}" data-price="{{ product.price }}">
      {% include 'product-listing' %}
    </div>
    {% endfor %} 
  </div>
  {% endif %}
  {% endfor %}

【问题讨论】:

    标签: html shopify liquid


    【解决方案1】:

    这是我想出来的。

    {%- assign dates = '' -%}
    {%- assign datesHandles = '' -%}
    
    {%- for product in collections.all.products limit: 20 -%}
        {%- assign createdAt = product.created_at | date: '%s' -%}
        {%- assign dates = dates | append: createdAt | append: ',' -%}
        {%- assign datesHandles = datesHandles | append: product.handle | append: '@' | append: createdAt | append: ',' -%}
    {%- endfor -%}
    
    {%- assign dates = dates | split: ',' | sort | reverse -%}
    {%- assign datesHandles = datesHandles | split: ',' -%}
    
    {%- for orderedDate in dates -%}
        {%- for dateHandle in datesHandles -%}
            {%- assign dateHandleArr = dateHandle | split: '@' -%}
            {%- if orderedDate == dateHandleArr[1] -%}
                {{ all_products[dateHandleArr[0]].title }}<br/>
                {% break %}
            {%- endif -%}
        {%- endfor -%}
    {%- endfor -%}
    

    问题是您受到 all_products 的限制,它只允许 20 个唯一的句柄请求。

    所以这段代码将只显示按创建日期订购的前 20 个产品,我不知道它是否对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多