【问题标题】:Detect whether a product is new. Shopify Liquid检测一个产品是否是新的。 Shopify 液体
【发布时间】:2017-06-23 03:53:21
【问题描述】:

我希望能够在最近添加到 shopify 中的产品旁边显示文本“新”。

类似{% if product.date_added > ten_days_ago %}New{% endif %}

编辑

似乎有一些product 的未记录对象,我通过控制台日志记录{{ product | json }} 发现了这些对象。

{{ product.created_at }}
{{ product.published_at }}

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    你可以这样做。

    {% assign product_date = product.published_at | date: "%s" %}
    {% assign current_date = 'now' | date: '%s' %}
    {% assign difference = current_date | minus: product_date | date: "%d" %}
    
    {% if difference < 10 %}
        New
    {% endif %}
    

    【讨论】:

    • 我想我可以使用 %j 作为日期,即一年中的某一天,从字面上看,如果是 1 月 1 日,则日期为 1。
    • @Buts 是的,这也可以。代码将相同,但它将删除最终日期过滤器date: "%d"
    • 实际上你是对的 "%s" 因为它是几秒钟后!这意味着我不必担心跨年。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多