Product Metafield Definition Question: “Select Type: Product”

Solved
colleenc
Shopify Partner
82 7 19

I am trying to code if "this" matches "that" then do this. I am using a custom product metafield connected to a product. When I chose "product" as the metafield type, and select a product in that field, what is Shopify storing? What do I reference on the product side to compare to it?

Use Case: In the metafield I am selecting a product that will be a gift included with the purchase of a specific variant of the product. I want to add a line to the product card that will indicate when stock is low on the gift with purchase item. I have written some custom liquid that I thought would work, but it isn't working as I can't figure out what I need to reference on the gift's product side. I hope that makes sense. I have added the metafield definition at the bottom for clarity.

 

The line that I think is failing is:

 

 

{% if product.id == product.metafields.custom.gift_with_purchase %}

 

 

I have tried product.id and product.title but neither of those works. I have read the documentation, and I am still unclear as to what the info metafield is storing so I can match it.

 

Here is the code I tried adding to the product card:

 

{% for product in collections.all.products %}
  {% if product.id == product.metafields.custom.gift_with_purchase %}
    {% assign gift_inventory = product.inventory_quantity %}
  {% endif %}
{% endfor %}

{% if gift_inventory < 1000 %} 
  <p>Low stock on gifts! Hurry up!</p>
{% endif %}

This is how I set up the metafield:

 

Screenshot 2023-05-16 at 3.14.46 PM.jpg

Accepted Solutions (2)
abhiteshs
Shopify Staff
Shopify Staff
5 3 3

This is an accepted solution.

Hi,

 

If I'm understanding your requirements correctly, you may need to modify the liquid code's if statement with code that looks like this::

{% if product.id == product.metafields.custom.gift_with_purchase.value.id %}

 Let me know if it works.

 

Thanks

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

PaulMoran
Shopify Staff
Shopify Staff
1 1 1

This is an accepted solution.

Abhitesh's code above is correct for accessing the linked product. I just wanted to clarify about the use case.

Lets say you have a Product called Nike Runner and you then have your product reference metafield definition setup called `gift_with_purchase`.

You go into the Nike runners product and scroll to the metafields section and you choose a product to link to. You mentioned this gift would be given away with the Nike Runners. Lets say its a pair of laces. So in the Nike runners product you select the laces product as the metafield.

If you simply want to check the inventory levels of the linked product you shouldn't need to loop through collections.all.products. As you just want to return the product object for the linked product (laces) so you can get the inventory level. So I would do that something like this:

{% assign gift_product = product.metafields.custom.gift_with_purchase.value %}
<p>Free Gift: {{ gift_product.title }}</p>

{% assign gift_inventory = gift_product.variants.first.inventory_quantity %}
{% if gift_inventory < 1000 %}
  <p>Hurry, there are only {{ gift_inventory }} free gifts left!!</p>
{% endif %}


Tested on my store on the main-product.liquid page and confirmed it worked. Heres a snipppet for context of where I added the code.


Note: {% assign gift_product = product.metafields.custom.gift_with_purchase.value %} is assigning the laces product object to the variable gift_product. So if you just wanted the id or title you could bypass assigning to a variable and do product.metafields.custom.gift_with_purchase.value.id or product.metafields.custom.gift_with_purchase.value.title to access the specific values.

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 5 (5)
abhiteshs
Shopify Staff
Shopify Staff
5 3 3

This is an accepted solution.

Hi,

 

If I'm understanding your requirements correctly, you may need to modify the liquid code's if statement with code that looks like this::

{% if product.id == product.metafields.custom.gift_with_purchase.value.id %}

 Let me know if it works.

 

Thanks

To learn more visit the Shopify Help Center or the Community Blog.

colleenc
Shopify Partner
82 7 19

I think you have understood what I am trying to do. Unfortunately, it did not work. 

PaulMoran
Shopify Staff
Shopify Staff
1 1 1

This is an accepted solution.

Abhitesh's code above is correct for accessing the linked product. I just wanted to clarify about the use case.

Lets say you have a Product called Nike Runner and you then have your product reference metafield definition setup called `gift_with_purchase`.

You go into the Nike runners product and scroll to the metafields section and you choose a product to link to. You mentioned this gift would be given away with the Nike Runners. Lets say its a pair of laces. So in the Nike runners product you select the laces product as the metafield.

If you simply want to check the inventory levels of the linked product you shouldn't need to loop through collections.all.products. As you just want to return the product object for the linked product (laces) so you can get the inventory level. So I would do that something like this:

{% assign gift_product = product.metafields.custom.gift_with_purchase.value %}
<p>Free Gift: {{ gift_product.title }}</p>

{% assign gift_inventory = gift_product.variants.first.inventory_quantity %}
{% if gift_inventory < 1000 %}
  <p>Hurry, there are only {{ gift_inventory }} free gifts left!!</p>
{% endif %}


Tested on my store on the main-product.liquid page and confirmed it worked. Heres a snipppet for context of where I added the code.


Note: {% assign gift_product = product.metafields.custom.gift_with_purchase.value %} is assigning the laces product object to the variable gift_product. So if you just wanted the id or title you could bypass assigning to a variable and do product.metafields.custom.gift_with_purchase.value.id or product.metafields.custom.gift_with_purchase.value.title to access the specific values.

To learn more visit the Shopify Help Center or the Community Blog.

colleenc
Shopify Partner
82 7 19

Thank you Paul! You understood what I was attempting to explain - I will add a more concrete example in the future, yours was perfect. This is exactly what I needed and the added bonus is it is working in a custom liquid box in the template, so no heavy lifting in the code is necessary - although I need to add a bunch more if loops to this (start date, end date, variant that includes the gift) so I may just add it to the code regardless. 

I appreciate your help on this, I will be able to use this as a template for the rest of the if loops.

colleenc
Shopify Partner
82 7 19

I read right over "value" in your code sample. You were correct. Thanks!