How to tell if current page is Shopping Cart Page (in Javascript)?

Matt28
Shopify Partner
14 0 0

Hi guys,

Just wondering if there is a way to determine whether the current page is the "Shopping Cart" page in Javascript?  Can I assume all shopping cart pages will be /cart, or is it more complicated than that?

Thanks!

Matt.

0 Likes
Jason
Shopify Expert
10039 119 1876

The easiest way would be to just look at the current url - being /cart. Now some themes don't often direct users to that page if they use AJAX carts so you'd need to adapt your approach in that instance.

What's the use case?

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Matt28
Shopify Partner
14 0 0

Thanks Jason - I'd like to do some checking on my server (via Ajax call) if the user is hitting the cart page... would have preferred to do it on the checkout page, but alas, no javascript is allowed on there from what I've read :)

It's easy to check for the "/cart" url, but as you mentioned, in the case of other implementations of the cart, it may be an issue.

Thanks,

Matt.

0 Likes
bchesler
Tourist
3 0 0

Just in case anyone else lands here, you can request the page type. In this case:

{% if request.page_type == 'cart' %}
    do something
{% endif %}

https://shopify.dev/docs/themes/liquid/reference/objects/request#request-page_type

0 Likes