I am trying to add a variant option (variant.option2) to my product titles collection listing pages in product.block.liquid.
How can I do it, and is it even possible?
I have added the following, but variant information does not show up:
<a class="title ftc" href="{{ url }}">{{ product.title }} - {% if product.variants %} {{ variant.option2 }} {% endif %}</a>
Generally, a product may have several variants. Do you want color for all of these variants printed out? Or any particlar variant? Or your products have only one choice of color?
That said, this should show a value of the second option of the first available variant:
<a class="title ftc" href="{{ url }}">{{ product.title }} - {% if product.variants %} {{ product.first_available_variant.option2 }} {% endif %}</a>
Just move that dash to be after {% if product.variants %}
If you want to show the current variant's color along with the product title, you should use this approach (if Color is your second option):
{{ product.title }} {% if product.selected_variant %}- <scope id="current_color">{{ product.selected_variant.option2 }}</scope> {% endif %}
To update it dynamically, you have to add this line to your selectCallback (selectCallback1 in troy's case) function after if( variant ) {
$('#current_color').text( variant.option2 );
You're right, because initially if you go to the product page (no varian in URL) there will be no selected_variant, liquid will not output the scope and javascript will be unable to update it.
To fix it, we will change liquid to always output the scope, only make it empty if there is no variant_selected:
{{ product.title }}<scope id="current_color">{% if product.selected_variant %} - {{ product.selected_variant.option2 }} {% endif %}</scope>
And javascript:
$('#current_color').text( ' - ' + variant.option2 );
User | Count |
---|---|
746 | |
142 | |
102 | |
64 | |
46 |