首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在收藏页面中按发布日期对产品进行排序-购物

在收藏页面中按发布日期对产品进行排序-购物
EN

Stack Overflow用户
提问于 2018-02-21 20:49:49
回答 1查看 998关注 0票数 1

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

我相信这在Shopify是可能的,我只是不明白我错过了什么。

代码语言:javascript
复制
  {% 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 %}

EN

回答 1

Stack Overflow用户

发布于 2018-02-22 15:55:12

这就是我想出来的。

代码语言:javascript
复制
{%- 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个产品,我不知道它对您是否有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48906388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档