Access Product Object and match to metafields data

Hi all

I have a page template which has some metafields attached and one of these contains a product handle, which I was using to pull in that products data and this worked by accessing all_products then appending the handle stored in the metafield with [metafield_name]:

{% assign product_handle = page.metafields.namespace.key %}> > {% assign product = all_products[product_handle] %}

all_products obviously has its limits so I need another way to go through the products in the collection and match the metafield data to products in that collection but after a few hours I’m drawing a blank.

I need to loop through products in a single collection, check if each one matches the handle in the metafield and set a variable saying its a match and check its available with product.available to alter some content if it sells out/ gets turned off.

I have this:

{% for product in collections.collection_name.products %}> {% if product.available and product.handle == prod_handle %}> {% assign product_active = true %}> {% endif %}> {% endfor %}

This returns true and forcing a handle that isn’t in the collection returns false… but I cannot access any of the product properties, like product.title etc?

Hi @anton_nv ,

Please change code:

{% assign product_handle = page.metafields.namespace.key %}
{% for product_collection in collections['collection handle'].products %}
  {% if product_collection.available and product_collection.handle == product_handle %}
  	{% assign product_active = true %}
  {% endif %}
{% endfor %}

Hope it helps!