Shopify/liquid - adding classes and removing injected text - shopify

I'm working on a Shopify (slate) theme with very limited experience. There's a few classes I need to add which I've managed on images but can't find a way to do this on an anchor included within <h2 class="mosaic__title p-name">{{ item.title | link_to: item.url }}</h2>.
Also I have some prices that has text like "On Sale from" injected before it. I don't know how this is added or how to remove it. Here's an example:
<del>{{ item.compare_at_price | money }}</del>
{% assign sale_price = item.price | money %}
{{ 'products.product.on_sale_from_html' | t: price: sale_price }}
I've tried removing the .on_sale_from_html and t: price: sale_price but that doesn't work/it breaks.
Can anyone advise on this? Thanks!
Full section of code for reference:
<div class="mosaic__caption">
<h2 class="mosaic__title p-name">{{ item.title | link_to: item.url }}</h2>
{% if item.object_type == 'product' %}
<p class="mosaic__value">
{% if item.compare_at_price > item.price %}
{% if item.price_varies %}
<del>{{ item.compare_at_price | money }}</del>
{% assign sale_price = item.price | money %}
{{ 'products.product.on_sale_from_html' | t: price: sale_price }}
{% else %}
{{ 'products.product.on_sale' | t }}
<data itemprop="price" class="p-price">{{ item.price | money }}</data>
{% endif %}
<data class="visually-hidden p-price">{{ 'products.product.regular_price' | t }}</data>
{% else %}
{% if item.price_varies %}
{% assign price = item.price | money %}
<data itemprop="price" class="p-price">{{ 'products.product.from_text_html' | t: price: price }}</data>
{% else %}
<data itemprop="price" class="p-price">{{ item.price | money }}</data>
{% endif %}
{% endif %}
{% unless item.available %}
{{ 'products.product.sold_out' | t }}
{% endunless %}
</p>
{% else %}
<p>{{ item.content | strip_html | truncatewords: 50 }}</p>
{% endif %}
</div>

Lets start with the link_to filter. This code: <h2 class="mosaic__title p-name">{{ item.title | link_to: item.url }}</h2>
The link_to takes a URL and just creates a html link element with the provided text and link.
So the above code is the same as:
<h2 class="mosaic__title p-name">
{{item.title}}
</h2>
So you can write as an alternative the above code or use replace filter to add a class attribute like so for example: item.title | link_to: item.url | replace: '<a', '<a class="foo"'
As for your second question such outputs {{ 'products.product.on_sale_from_html' | t: price: sale_price }} points that this is a translatable text.
This means that your text is located in your translation file ( usually en.default.json in your locales folder ) so you can modify the text from there.
As for the text that is added, it seems that your translated string contains the following {{ price }} variable which is replaced via the passed variable price: sale_price.
PS: Read up the documentation in Shopify where these functions are described in more detail:
https://help.shopify.com/themes/liquid/filters/url-filters#link_to
https://help.shopify.com/themes/development/internationalizing/locale-files#values

Related

Concatenate string with html in liquid Shopify

{% assign price = product.price | money %}
{% capture dot_separator %} <span class='Button__SeparatorDot'></span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}
My goal is to show the "addToCartText" text this way
Goal: ADD TO CART . 287$
Where the dot is created using css
Right now it's showing like this
Current output: ADD TO CART < span class='Button__SeparatorDot'></ span> 287$
How would I tell liquid to read the html as it is?
Try to use strip_html filter https://shopify.github.io/liquid/filters/strip_html/
like so:
{% capture dot_separator %} < span class='Button__SeparatorDot'></ span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator | strip_html }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}

how to get highest number of loop elements in shopify

how can I get highest numbers from loop here is my code
from this code I got 2,1,3,1,2
So I want 3 as a result could you please help me to solve this problem any help would be appriciated
{% for line_item in cart.items %}
{% assign c_no = line_item.properties | join:',' | remove_first : 'meal pack,' | strip | split : '-' | last | sort: 'price' %}
<h1>{{c_no}}</h1>
{% endfor %}
Use the {{ forloop.length }}
{% for product in collections.frontpage.products %}
{% if forloop.first %}
<p>This collection has {{ forloop.length }} products:</p>
{% endif %}
<p>{{ product.title }}</p>
{% endfor %}
Docs: https://shopify.dev/api/liquid/objects/for-loops

Display name of colour variant selected on Shopify

On this page I have some swatches that when selected they don't display the variant name.
Basically, I need to add a paragraph text under the variant swatches that indicate the name of the variant selected...
I'm not good on javascript and online I didn't found any complete guide about that
Thanks a lot for your help!
That looks like a theme I've seen before. If I recall correctly, that theme uses a file called swatch.liquid to make those colour circles. I'm writing this answer using a version of a swatch file that I happen to have on hand, so the specific code may not be exactly what your theme has, but hopefully I'll be able to help you find what you need.
Here is the code from my swatch.liquid file that prints out the swatches. (This is roughly the bottom half of the swatch.liquid file, found in the snippets folder)
<div class="product-options">
<div class="swatch clearfix" data-option-index="{{ option_index }}">
<div class="header">
{% assign values = '' %}
{% for variant in product.variants %}
{% assign value = variant.options[option_index] %}
{% unless values contains value %}
{% assign values = values | join: ',' %}
{% assign values = values | append: ',' | append: value %}
{% assign values = values | split: ',' %}
<div data-value="{{ value | escape }}" class="swatch-element {% if is_color %}color {% else %}size {% endif %}{{ value | handle }} {% if variant.available %}available{% else %}soldout{% endif %}">
<input id="swatch-{{ option_index }}-{{ value | handle }}" type="radio" name="option-{{ option_index }}" value="{{ value | escape }}"{% if forloop.first %} checked{% endif %} {% unless variant.available %}disabled{% endunless %} />
{% if is_color %}
<label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: {{ value | split: ' ' | last | handle }};">
<img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
</label>
{% else %}
<label for="swatch-{{ option_index }}-{{ value | handle }}">
{{ value }}
<img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
</label>
{% endif %}
</div>
{% endunless %}
{% if variant.available %}
<script>
jQuery('.swatch[data-option-index="{{ option_index }}"] .{{ value | handle }}').removeClass('soldout').addClass('available').find(':radio').removeAttr('disabled');
</script>
{% endif %}
{% endfor %}
</div>
</div>
</div>
That looks a bit messy, I know - so let's try and find the part that you'll want to modify.
Here's the part from the above example that prints the swatch element when the swatch is a colour:
{% if is_color %}
<label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: {{ value | split: ' ' | last | handle }};">
<img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
</label>
{% else %}
In between those <label> tags you should be able to add any text that you need - such as a <span class="swatch-color-name">{{ value }}</span>
Adding extra HTML in here may throw off the current styles of your page, so you may need to add some extra CSS rules to apply to any elements you add to get things to line up nicely again.
Hope this helps!
NB: If you're using Chrome, there's a useful extension called "Search All" that lets you search your entire Shopify theme if you're having trouble finding the file that needs to be edited. Searching for keywords like 'color' or 'variant.options' should help point you in the right direction.

Shopify Liquid. If line.title contains '<br>' statement is not working

We are using Shopify where we have an invoice page where product title and other data is showing.
My problem is that product.title is displaying <br> tag on the invoice page.
(On the product page title is displaying w/o <br> tag)
The reason for this is that product.title can only be entered via WYSIWYG. Where WYSIWYG encodes product.title into ASCII code and decodes product.title on the invoice page.
I am trying to find and replace/hide <br> tag inside product.title using Liquid string filter {{ product.title | replace: 'Awesome', 'Mega' }} with {% if %} statement and it is not working. If i'll change {% if line.title contains '<br>'%} to {% if line.title contains 'Dress'%} the code will replace Dress with white space.
Kindly find below part of my code.
Part of the code i added
{% if line.title contains '<br>'%} {{ line.title | replace: '<br>', ' ' }} {% endif %}
General code
<td class="order-list__product-description-cell" style="width:100%">
{% if line.product.title %} {% assign line_title = line.product.title %}
{% else %} {% assign line_title = line.title %} {% endif %}
<span class="order-list__item-title">
<!-- Replace if statement -->
{% if line.title contains '<br>'%} {{ line.title | replace: '<br>', ' ' }}
{% endif %}
<!-- Replace if statement -->
{{ line_title }} × {{ line.quantity }}
</span>
<br/> {% if line.variant.title != 'Default Title' %}
<span class="order-list__item-variant">{{ line.variant.title }}</span> {% endif %}
I would highly appreciate if you could please tell me what am i doing wrong and guide me on how to implement this in the right way.
Meanwhile waiting for Shopify to fix the issue of displaying tags in their app
If your title is showing like this:
This is a really, really, really,<br>really long product title!
Then Shopify is probably escaping your HTML tag, turning it into this under-the-hood:
This is a really, really, really,<br>really long product title!
If you update your {{ product.title }} code to: {{ product.title | replace: '<', '<' | replace: '>', '>' }} you should get the following:
This is a really, really, really,
really long product title!
Alternatively, if you want to remove the line break entirely, you could use either:
{{ product.title | replace: '<', '<' | replace: '>', '>' | remove: '<br>' }}
or:
{{ product.title | replace: '<br>', ' ' }}

How to display "x-y products of z" in Shopify collections

I'd like to be able to display the text "Showing 1-24 of 242 products" (or "Showing 25-48 of 242 products" etc.) on each collection page of a Shopify site.
I thought there'd be a simple bit of Liquid code to do this but either my Google-foo is very weak, or it doesn't exist.
Does anyone have a snippet of code they can share to achieve this?
Your Google-foo somehow missed the obvious, which is a search of the Shopify documentation on how Shopify works. Try this for the answer to your question:
Pagination
We have this:
{% if collection.products.size > 0 %}
<span><h1 class="title">{{collection.title}}</h1></span> {% if current_tags %}>> {{ current_tags.first }}{% endif %} {{tag}} ({{ paginate.current_offset | plus: 1 }} -
{% if paginate.next %}
{{ paginate.current_offset | plus: paginate.page_size }}
{% else %}
{{ paginate.items }}
{% endif %} of {{ paginate.items }})
{% if collection.description.size > 0 %}
<p>{{ collection.description }}</p>
{% endif %}
{% else %}
<h1>{{collection.title}}</h1> <span>{{tag}} (0 results)</span>
{% endif %}
Check the official Pagination Shopify Docs.
The following piece of code:
Showing items {{ paginate.current_offset | plus: 1 }}-{% if paginate.next %}{{ paginate.current_offset | plus: paginate.page_size }}{% else %}{{ paginate.items }}{% endif %} of {{ paginate.items }}.
to output something like this:
Showing items 26-50 of 345.