如何检查我们是否在购物车页面Shopify

如何检查我们是否在购物车页面Shopify,shopify,Shopify,如何检查当前页面是否是theme.liquid中的购物车页面?我尝试了page.handle==“cart”,但它不起作用。我找到了解决方法 使用{{template | handle}} 所以我的代码是 {% capture template_handle %}{{ template | handle }}{% endcapture %} {% if template_handle == 'cart' %} {% endif %} 您不需要句柄过滤器,只需使用: {% if templa

如何检查当前页面是否是theme.liquid中的购物车页面?我尝试了
page.handle==“cart”
,但它不起作用。

我找到了解决方法

使用
{{template | handle}}

所以我的代码是

{% capture template_handle %}{{ template | handle }}{% endcapture %}

{% if template_handle == 'cart' %}

{% endif %}

您不需要
句柄
过滤器,只需使用:

{% if template == 'cart' %}
  ...
{% endif %}

此外,我们还可以使用以下方法进行检查:

{% if template contains 'cart' %}
...
{% endif %}

两个答案都不是绝对正确的,包括公认的答案

购物车模板与其他模板一样可能有备选模板,例如可以通过
/cart?view=fancy
访问的
cart.fancy.liquid
。在这种情况下,相等运算符将无法按预期工作,因为
模板
变量将等于
cart.fancy

contains
运算符也不是最佳选项,因为对于
product.cartridge.liquid
和类似模板,它也将是TRUE

基于上述情况,以下是在主题中使用的更好的解决方案:

{%- assign templateBase = template | slice: 0, 4 -%}
{%- if templateBase == 'cart' -%}
  ...
{%- endif -%}
  • 对于购物车模板及其所有备选视图,它将是真的
  • 对于任何可能包含
    cart
    按后缀顺序排列,即备用视图

这是另一个选项:

{% if request.path == routes.cart_url %}
  We are on the cart page.
{% else %}
  We are on some other page.
{% endif %}

很好,谢谢你分享这条宝贵的信息