0

I'm trying to edit a section in my Shopify theme which displays a video. I'm using the section on product pages and the URL for the video is inserted by using the theme editor tool. As such the same video inserted will show on every product page using that template. However, I would like to use a different video on each product page. I can't see a way to do this through the theme editor unless I duplicate the template for each and every product that needs its own video URL (seems a bit overkill).

I found this code which controls the video section, and the bit I'm trying to figure out is how I can change the 'assign video_id' to use a metafield so that I can add this per product by simply adding the video URL on a specific product page. I can't think of an easier way to achieve what I need, but thought changing a metafield for each product that needs a custom video URL would be the best way to do it, working possibly similar to this:

{% if template.name == 'product' %}
 {% if product.metafields.my_fields.product_video_url %}
   {{ product.metafields.my_fields.product_video_url }}
 {% endif %}
{% endif %}

If anyone had an idea of how I can do this using the code below and inserting a metafield (and even better, a metafield IF it's filled, otherwise default back to video_url.id) I would really appreciate it.

{%- liquid
  assign bg_color = section.settings.background-color
  assign button_text = section.settings.button-text | escape | truncate: 30
  assign button_url = section.settings.button-url | url_escape
  assign full_width = section.settings.full-width
  assign heading = section.settings.heading | escape
  assign darken_video = section.settings.darken-video
  assign light_text = section.settings.light-text
  assign section_height = section.settings.section-height
  assign sub_heading = section.settings.sub-heading | escape
  assign thumbnail = section.settings.image
  assign video_url = section.settings.video-url

  if video_url.id
    assign video_id = video_url.id
  else
    assign video_id = '_9VUPq3SxOc'
  endif

  assign cover_link = false
  if button_text == blank and button_url != blank
    assign cover_link = true
  endif

  assign button_type = 'button'
  if light_text
    assign button_type = 'inverted-secondary-button'
  endif
-%}

Thank you for any help at all.

1 Answer 1

0

Metafields are pretty simple, and very useful. First off, you can set them in the Admin. Create one with a description.

Note the concept of my_fields is really dumb, instead, think of it as "context", the "what am I in all this". So you might use a namespace here, like "product_videos". Hence, at this point, think of it as a box named, with whatever is inside the box, still a mystery!

product.metafields.product_videos

Or your company name. Or your cat's name. Or whatever turns your crank. my_fields is just generic filler. Means nothing.

Once you get over that hump, that you've now got a namespace, you can actually get down to the nitty-gritty. Anything of value to you is likely to be a string here, a string representing a video. So you want to make the type of the Metafield to be text. A string of text.

To be of much use, you need a key. You want to grab onto the key! So if a product has a special movie, let the key guide you right to it! The key is going to be something brilliant like; video_id! So now you have a product, with a Metafield resource, in your namespace, product_videos, with a key! Namely video_id. And you can then go to that product in your Shopify Admin, and at the bottom of the details page, fill in the answer to the question of video_id. Give it some info. Where to find the video. What it is called. Anything useful to you.

Now in your theme, just reference {{ product.metafields.product_videos.video_id }} when you need it.

2
  • Thanks for the reply David. I'm familiar with using the metafields in templates, but the thing I'm not so sure of is how to use it in the above code, namely the line assign video_id = video_url.id. I would guess something like assign video_id = {{ product.metafields.product_videos.video_id }} but this outputs the text as-is shown, and not retrieving the actual data from the product metafield.
    – Justin B
    Jan 21, 2022 at 15:47
  • The thing is to save is a pointer to where the data is. That is up to you to figure out. No one except you knows where the data is. I just showed you the simple way to use metafields to organize your problem. You can now figure out the easy part, which is using the info about where a video is, to play it or whatever it is you need to do. Jan 21, 2022 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.