【问题标题】:How to check if we are in cart page Shopify如何检查我们是否在购物车页面 Shopify
【发布时间】:2015-03-17 04:13:24
【问题描述】:

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

【问题讨论】:

    标签: shopify


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      没有一个答案是绝对正确的,包括被接受的答案。

      与其他人一样,购物车模板可能有替代品,例如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
      • 对于任何可能包含 cart 后缀中的序列,即备用视图。

      【讨论】:

      • 很好,感谢您分享宝贵的信息
      【解决方案3】:

      这是另一种选择:

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

      【讨论】:

      • 这是我的最爱。
      【解决方案4】:

      我找到了解决方案。

      使用{{template|handle}}

      所以我的代码是

      {% capture template_handle %}{{ template | handle }}{% endcapture %}
      
      {% if template_handle == 'cart' %}
      
      {% endif %}
      

      【讨论】:

        【解决方案5】:

        我们也可以使用:

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

        【讨论】:

          猜你喜欢
          • 2022-10-19
          • 1970-01-01
          • 2021-05-15
          • 1970-01-01
          • 2011-04-09
          • 2020-08-06
          • 2018-10-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多