Solved

Why does my liquid code return an empty string for canonical URL?

Gonkas
Shopify Partner
11 0 1

Hi,

We are trying to return the true canonical value instead of an url with a search parameter.

So instead of having something like this:

[domain]/collections/jackets-coats?page=srnqqdjnmld

We would like to show something like this:

[domain]/collections/jackets-coats

Does anyone knows why the following code returns an empty string?

 

 {% assign canonicalUrl = {{ canonical_url }} %} 
 
 {% assign secondUrl = canonicalUrl | split: '?' | last %}
 {% if secondUrl != 'page=2' %}
 {% assign canonicalUrl = canonicalUrl | split: '?' | first %}
 {% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

 

 Code above works in a liquid sandbox.

Thanks.

Cheers,

Gonkas

Accepted Solutions (2)

LitCommerce
Astronaut
2860 684 736

This is an accepted solution.

Hi @Gonkas,

Please change code:

{% assign canonicalUrl = canonical_url %} 
 
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

Hope it helps!

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!

View solution in original post

isis-mtnez
Shopify Partner
25 3 2

This is an accepted solution.

The issue is extra {{  }}      {% assign canonicalUrl = {{ canonical_url }} %}

This is the correct code:   {% assign canonicalUrl =  canonical_url  %}

But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.

For example, for a product in a collection with a variant selected:

https://example.com/collections/classics/products/shoes?variant=17287731270

The canonical URL is the product page:  https://example.com/products/shoes

Have a great day!!!


- Was I helpful? Like to let me know 🙂

- Did I answer your question? Mark my reply as Accepted Solution

 

Give me a Like if I helped you
Please mark as Accepted Solution if I fixed your problem!
Thank you 🙂

View solution in original post

Replies 4 (4)

LitCommerce
Astronaut
2860 684 736

This is an accepted solution.

Hi @Gonkas,

Please change code:

{% assign canonicalUrl = canonical_url %} 
 
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}

<link rel="canonical" href="{{ canonicalUrl }}" />

Hope it helps!

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!
adamsweden
Shopify Partner
6 0 1

Shouldn't it be:

{% if secondUrl == "page=2" %}

Your code doesn't work for me, but after making that change it does.

isis-mtnez
Shopify Partner
25 3 2

This is an accepted solution.

The issue is extra {{  }}      {% assign canonicalUrl = {{ canonical_url }} %}

This is the correct code:   {% assign canonicalUrl =  canonical_url  %}

But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.

For example, for a product in a collection with a variant selected:

https://example.com/collections/classics/products/shoes?variant=17287731270

The canonical URL is the product page:  https://example.com/products/shoes

Have a great day!!!


- Was I helpful? Like to let me know 🙂

- Did I answer your question? Mark my reply as Accepted Solution

 

Give me a Like if I helped you
Please mark as Accepted Solution if I fixed your problem!
Thank you 🙂
Gonkas
Shopify Partner
11 0 1

Thanks both, it was extra {{ }}.

Shopify should work that out but it doesn't so needs this "hack".

 

Have a great day!