Get Shopify product title and variant title in same object

Get Shopify product title and variant title in same object

WillyStudent
Shopify Partner
1 0 0

Hi,

I want to get product title and variant title sent to Google Analytics like this "Product title - Variant title"

I managed this by doing the following:
'item_name': '{{ product.title }} - {{ product.selected_or_first_available_variant.title }}'

However when there was no variants the variant title defaulted to "Default title":
'Running Shoe - Default Title'

One solution might be to do something similar to:
'item_name': '{{ product.title }}{% if product.selected_or_first_available_variant.title != 'Default Title' %} - {{ product.selected_or_first_available_variant.title }} {% endif %}'

However it seems fragile. Any ideas for a better solution?

Thank you.

Reply 1 (1)

maxdanko
Shopify Partner
29 3 7

I think this might help.



{% unless product.has_only_default_variant %} - {{ product.selected_or_first_available_variant.title }}{% endunless %}