2
votes

For a product which does not belong to any collection we can have url defined as - <shop-url>/products/< product.handle >. In case a product belongs to a collection it changes to - <shop-url>/collections/<collection-name>/products/< product.handle >. Is there any third case also?

How to handle these cases. Also, I do not get the collection name attached to the product when I make an api call so that I may build upon the url myself. Is there a generic way to get the product url when fetching product via api call?

I wonder why shopify does not provide with the product url straightways! Is there any reason?

EDIT:

I just realized that even if a product is associated with a collection we can have its url as <shop-url>/products/< product.handle >; and <shop-url>/collections/<collection-name>/products/< product.handle > are same thing. Two different url but same page?

3

3 Answers

6
votes

In liquid you can access a product's URL with product.url, which outputs /products/handle. It doesn't look like the URL is available via the API, but you could easily build the URL with the product's handle (which you can get via the API).

1
votes

You are right, a product can have many URLS, that why it's important to include a canonical URL in the head of your products html.

0
votes

For those needing to build a link to a product within a collection by hand in liquid, here's the expression:

{% assign url = "/collections/" | append: collection.handle | append: "/products/" | append: product.handle %}

Also this filter works:

{{ product.url | within: collection }}