Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Custom, reusable product templates with 2.0 blocks

Custom, reusable product templates with 2.0 blocks

graemebryson
Shopify Partner
3 0 0

I'm attempting to a create a custom product template, but the current instance is not reusable across multiple products — any changes made to one product using the template is also made to all others using it.

 

I've uploaded a sample (simplified) template and section here for context on the current approach.

Template: https://gist.github.com/graemebryson/f51680b83d4e3a71a45186ed70daf00d
Section: https://gist.github.com/graemebryson/b4eb559d76c1981946c8bca3de160bd9

 

As I understand it, the problem occurs because the custom product data is being stored to the section — I've read that passing product data in a var to a snippet using the {% render %} tag allows you to target the current product only, but I can't place the schema required for the custom block(s) in a snippet.

 

I've scoured the forums and third-party resources for information on using custom blocks with product-specific content within one template for all products, but can only find old comments saying it wasn't possible at that time. Can anyone offer some clarification, or at least point me in the direction of some information specific to reusable product templates using custom 2.0 blocks?

 

Thanks,
Graeme

Replies 3 (3)

graemebryson
Shopify Partner
3 0 0

Adding some additional context to say metafields are not a viable solution to this; any one product page could could require upwards of 100 individual metafields to satisfy the amount of data required that would otherwise go into blocks. If anyone has any thoughts on how else to manage this I'd be massively appreciative.

david_phl
Shopify Partner
1 0 0

One thing I have done in cases like this, where management of the content isn't reasonable in metafields (like if there would need to be 100 individual ones), is to use another CMS (in my case sanity.io) to manage the additional content and relate it to Shopify products. The CMS calls a webhook when the content is modified. The webhook writes a JSON object with all the data into a Shopify metafield. Then I use liquid to read the JSON and output it as I need to in various snippets. I use the Mesa app to get the webhook data and write it to the Shopify metafield. 

 

You could of course use React or similar and have components fetching the data from the CMS as well. But the approach above has been relatively easy for me once I get things setup and then the content is all in the generated product page markup.

 

There is an app that connects Sanity to Shopify which makes it easy to extend the Product schema in Sanity to have whatever kind of fields you need. And the content management experience for users on Sanity is great...very easy to manage a lot of data and different data types.

Trudy
Shopify Partner
11 1 4

I would suggest using MetaObjects (which is conceptually the same as metafields) because they're way easier for management (ie you could put all the data for each section in 1 MetaObject and you can re-use the MetaObjects (say for FAQs where they may be the same across products)

 

I've been creating a playlist of all the things you can do with MetaObjects https://www.youtube.com/playlist?list=PL8jYlLhLXeM3lF1RsXz_wv5ZQwGw3yFZM

Looking to customize your theme? Don't hire a developer - instead check out Design Packs! https://apps.shopify.com/design-packs