Reputation: 35
I created breadcrumb code for my client's Shopify site that displays
collection >> product tag >> product
For example:
tableware >> plates >> yellow-striped plates
We use product tags to create subcategories. The only scenario where this breadcrumb doesn't work is when one product is tagged for two different subcategories under a collection. So for example, we have the collection "party favors", and that has as subcategories (product tags) "girls favors" and "boys favors". Wooden spinning tops is tagged both "girls favors" and "boys favors". So when going through the array, "boys favors" comes up first and displays on the product page both when user comes from the girls favors subcategory and the boys favors subcategory. I can't use a remove to remove "boys favors" as that will remove it for the boys favors product page as well.
Is there a way my breadcrumb.liquid page can know which collection/producttag URL a user came from, and then can I take that info and make a variable I can create conditional code around to display the correct product tag (in a case where the same product has two different product tags representing two subcategories under the same collection)?
Upvotes: 0
Views: 1523
Reputation: 2925
Shopify doesn't store variable data. It renders the page solely based on the URL. You can use a combination of JS and liquid code to get this working
collection.liquid
template or product-block.liquid
snippet find a
tag that is linked to product.href
attribute add this code {% if current_tags %}#{{ current_tags }}{% endif %}
theme.liquid
layout, right before <\head>
add this line <script>var c_tag = decodeURI(window.location.hash)</script>
<span id='r_tag'>boys favors >></span>
.Right after that span add this:
<script>if(c_tag == null || c_tag == $('#r_tag').innerHTML.replace(' >>','')){}
else { $('#r_tag').innerHTML = c_tag + ' >>' }</script>
Please note that if there are more than 1 current_tags
you'd have to modify the code accordingly.
Upvotes: 1