How can I update a metafield with a variable for image and text sections?

How can I update a metafield with a variable for image and text sections?

digialkrouse
Excursionist
15 2 5

I have a metafield that I'm using to populate an image-and-text section. The image-and-text section shows a photo of the designer alongside the designer's bio.

 

The designer photos and bios will change over time. Is there a way to enter a variable into the metafield so that in the future, I can update the photos and bios in one place — wherever the variable is defined — instead of having to update the metafield section of every product by that designer?

 

Screen Shot 2022-09-13 at 3.54.50 PM.png

Replies 2 (2)

made4Uo
Shopify Partner
3873 718 1221

Hi @digialkrouse,

 

This would be a great feature to have. It would be nice to add the metafield using the multiple edits but unfortunately, as of now, this is not possible

If this fixed your issue Likes and Accept as Solution is highly appreciated. Coffee tips fuels my dedication.
Get EXPERIENCED Shopify developers at affordable rates—visit Made4Uo.com for quick quote!
Do not lost your Shopify store! Get FREE trial with ✔️ Rewind Backup: Automatic, reliable, stress-free

PaulNewton
Shopify Partner
7722 678 1626

Hi @digialkrouse Tons of approaches here, bios should probably just be pages.

 

No you cannot metaprogram metafields by putting variables inside a metafield.

But metafields do have square bracket notation to access namespaces and keys using variable strings inside liquid i.e. {{ product.metafields[namespace][key] }}  and a JSON type that then kinda lets you precreate liquid objects.

Understanding Metafield Behavior

Metafield data is dynamic per resource(collection,product,etc) with the value being unique set to the singular thing it's attached to not shared among every resource using that metafield. Meaning there is no native sync to have the same values in sets of metafield definitions(mfdefs)

This means they behave just like a products description, other products obviously don't share that description-content unless it's purposefully copied. The sight technical exception here being the single-text type metafield definition that can have preset choices, but the choices still have to be selected per resource. 

Managing Metafield Sync

For lots of sets of resources that need data in a metafield definition to be the same, in sync, merchants should use bulk editing tools and apps. Such as  ez-exporter, matrixify. Or scriptable automation tools like usemechanic to sync sets of metafields & content to be the same. In cases of sync automation the source of truth has to be indicated, i.e. which product description do all other products reference to sync their description too.

 AFAIK the shopify-flow is not capable of a storewide mfdef update sync like this only on single resources.

Alternative Content Information Architectures

While mfdefs provide a "codeless" convenience through dynamic sources in theme settings there a other ways to consolidate content management. Such as a bio being be a page.

Though most methods require a matching theme customization to render the right object properties per the business logic.

 

Since the intended content isn't dynamic per resource(collection,product,etc) then it should probably exist as different more static information feature in shopify to minimize content editing in a single spot.

  • Pages
  • Theme snippets
  • Theme settings
  • etc

A page itself can have it's own mfdefs such as to hold specific images or video, designer quote, etc 

While storing content in themes should be avoided for longterm sanity in some cases it can help to put such content in either unique theme snippets or theme settings.

 Theme settings can be applied to sets of specific resources using alternate templates.

https://help.shopify.com/en/manual/online-store/themes/theme-structure/templates#create-a-new-templa... 

 With theme settings the custom-liquid blocks|section in OS2.0 compatible themes can be extremely useful here such as with the untested liquid code in the sample section at the end of this post to pull a page content based on a product metafield.

 

In tandem with this you may want to hide such pages from external and internal search results. 

Making connections/relationships between systems

In the /page method you could then still utilize mfdefs as a page resource to indicate what pages content to use but now there is only a single place to edit the content. Though the initial setup of those references has to still be done.

If putting the content in a page you could still use a page reference metafield to indicate which resource(collection,variant,etc) uses which pages static content. i.e {{ product.metafields.content.bio.value.content }}

 

For products,blogs,etc there's using tags instead of metafields as the indicator for the relation between a designer and things like a product.

Such as in products using tags to indicate a page handle to use to pull content from either directly, or the mfdefs attached to that page so it's basically serving as a container for mfdefs.

The tag can hold the designers name in a convention ,example  "_bio_terry_pratchett" matching a page handle to pull in content using the all pages object in liquid;  for up to 50 pages without pagination.

 

 

Those are some main ways for now;  though there are some CMS changes supposedly coming but I wouldn't wait on such things. 

 

Sample Liquid Implementation

For use in a snippet or custom-liquid theme setting.

UNTESTED Always backup themes before changing code

 

 

{%- comment -%}Copyright Paul Newton 2022{%- endcomment -%}
{%- liquid 
 #config 
 ## rename variables as needed for understanding
 ###this part must be outside any loop calling this code

 #change resource_object to the relevant resource variants,customers,etc that has either tags or metafields
 assign resource_object = product

 assign css_class = "bio"
 #unused assign css_class_2 = "bio-headshot"

 # tag based references ,comment out if unused
 ## example tag "bio_dr-suess" where removing "bio_" gives a handle
 assign tag_prefix = "bio_"
 assign page_handle = ""

 # build metafields
 assign mf_namespace = "content"
 assign mf_key = "designer-bio-page"
 assign mf_key_2 = "designer-headshot"

 assign mf = resource_object.metafields[mf_namespace][mf_key]
 assign mf_2 = resource_object.metafields[mf_namespace][mf_key_2]

 # the page
 assign page = mf.value
-%}

{%- liquid
 if page == empty or mf.value == blank or mf.type != page_reference
  if resource_object.tags contain tag_prefix
   for tag in resource_object.tags
    if tag contain tag_prefix
     assign page_handle = tag | split: tag_prefix | last
     # remember limit of 50 pages without pagination
     assign page = pages[page_handle]
     #should the resource_object not have a mf_key_2 value try to default to the pages key
     assign mf_2 = mf_2 | default: page.metafields[mf_namespace][mf_key_2]
     {% break %}
    endif
   endfor
  endif
 endif
-%}

{% if page != blank %}
 <div class="{{css_class}} {{css_class | append:"_" | append:page-handle }}" >
  <p>{{ page.content }}</p>
  {% if mf_2 %}{{ mf_2.value | metafield_tag }}{% endif %}
 </div>
{% endif %}

 

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org