Trying to figure out a bit of liquid code and need some help. Some background:
I run a site for an author and each book has different “formats” (ebook, audiobook, paperback, hardback…etc). Normally I’d use Shopify’s “variants” for this, but can’t as there are reasons (most have to do with tax on things in the UK…don’t ask…). Suffice it to say I have to have each format as its own product.
However, I want to display all the “formats” on each of the product pages as a variant-style pill. I’d like to do it without having to reenter things 6-8 times for each product/format page.
I’ve got the following bit of code right now (omitting all the CSS/styling stuff):
{% assign items = product.metafields.custom.variant_products %}
{% if items.size > 0 %}
{% for i in items %}
{% assign handle = i %}
{% assign linked_product = all_products[handle] %}
{{ linked_product.type }}
{% endfor %}
{% endif %}
This bit references a single, product metafield (name=variant products) that is a simple, single line, multiple entry field of the handles of each product. One handle per entry/line.
It then displays each of the related formats as a variant pill. It works fine.
The problems with this approach (the site has more than 100 products) are:
#1 - I have to manually get the handles. Not a huge deal but time consuming.
#2 - I have to enter these in for each product, each time.
#3 - Potential for errors/typos with #1 and #2.
So what I’d like to do is use a metaobject…at least that’s what I am thinking so I can create the “sets” of variants once for each “product” and then reference it with a single product metafield.
Here’s what I’m thinking
-
Setup a meta object called “LinkedProductVariants”. In the Metaobject…only two values: Title and Formats.
-
Title is a “single line text” used as the title and name of the product. Formats is a “Product field”, (list of products that “become” the formats/variants).
-
Setup a new product metafield (name=LinkedFormats) that references the metafield. This will be set per product.
I’m good on all that.
So here are my questions.
I “think” I need to reference the new product metafield (since this is what’s in the product page) in this bit of code {% assign items = product.metafields.custom.variant_products %}. Is that correct?
If I need to reference the new product metafield since it references the metaobject, how do I pull the correct values from the metaobject (Formats)?
Once I do this I think I can use {% assign linked_product = product_metafield | handle %} to get the handles for each product.
Is there anything else I need to do with this setup to get the handle correct to make the link? Or…am I completely off base?
I hope this makes sense.
Still learning Liquid so appreciate any help.