user1917451
user1917451

Reputation: 169

How to check if we are in cart page Shopify

How can I check if the current page is the cart page in theme.liquid? I tried page.handle=='cart' but it is not working.

Upvotes: 6

Views: 12228

Answers (5)

publicapps
publicapps

Reputation: 190

This is another option:

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

Upvotes: 4

Vladimir
Vladimir

Reputation: 2559

Neither answer is absolutely correct, including the accepted one.

The cart template as others may have alternates e.g. cart.fancy.liquid which can be accessed via /cart?view=fancy. In that case, the equality operator will not work as expected as template variable will be equal to cart.fancy.

The contains operator is not the best option as well, as it will also be TRUE for product.cartridge.liquid and similar templates.

Based on the above here's the better solution to use in your themes:

{%- assign templateBase = template | slice: 0, 4 -%}
{%- if templateBase == 'cart' -%}
  ...
{%- endif -%}
  • It will be TRUE for cart template and all its alternate views.
  • It will be FALSE for any non-cart templates that may contain cart sequence in their suffixes i.e. alternate views.

Upvotes: 4

aldrien.h
aldrien.h

Reputation: 3635

Also we can check using:

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

Upvotes: 0

Steph Sharp
Steph Sharp

Reputation: 11682

You don't need the handle filter, you can just use:

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

Upvotes: 17

user1917451
user1917451

Reputation: 169

I found a solution for this.

using {{template|handle}}

so my code is

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

{% if template_handle == 'cart' %}

{% endif %}

Upvotes: 1

Related Questions