检测产品是否是新产品。 Shopify液体
问题描述:
我希望能够在Shopify中最近添加的产品旁边显示“New”文字。检测产品是否是新产品。 Shopify液体
喜欢的东西{% if product.date_added > ten_days_ago %}New{% endif %}
编辑
好像有是product
一些无证的对象,我发现通过控制台日志{{ product | json }}
。
{{ product.created_at }}
{{ product.published_at }}
答
你可以这样做。
{% 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
@Buts是的,这也能发挥作用。代码将是相同的,但它会删除最后日期过滤器'date:“%d”'。 – drip
其实你是对''%s“''因为它是秒!这意味着我不必担心跨越多年。 – Buts