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

How to check if we are in cart page Shopify

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

我找到了解决方案。

使用{{template|handle}}

所以我的代码是

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

{% if template_handle == 'cart' %}

{% endif %}

您不需要 handle 过滤器,您可以使用:

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

我们也可以检查使用:

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

两个答案都不是绝对正确的,包括被接受的答案。

其他购物车模板可能有替代品,例如cart.fancy.liquid 可以通过 /cart?view=fancy 访问。在这种情况下,相等运算符将不会按预期工作,因为 template 变量将等于 cart.fancy

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

基于以上内容,这里是您的主题中使用的更好的解决方案:

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

这是另一个选项:

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