按产品页面中的发布日期对产品进行排序 - SHOPIFY

时间:2018-02-21 12:49:49

标签: html shopify liquid

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

我确信在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 %}

1 个答案:

答案 0 :(得分:0)

这就是我想出来的。

{%- 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个产品,我不知道它是否对您有用。