How do I get the product object from all_products?

Solved
redshot
Tourist
12 2 2

Hi,

We are using an app called Accentuate Custom Fields to add custom fields to our products and other kinds of posts.

We want to display products that have been added from Accentuate Custom Fields on a certain type of Blog called Articles. In other words, there will be different display of products in every single article depending on what products were added/assigned from Accentuate Custom Fields.

I am able to display a non-repeating field assign to a specific article from Accentuate Custom Fields through this code {{ article.metafields.customsection.text }}

My current problem is that I want to get the product object by looping through the liquid code given by Accentuate. The liquid code given by Accentuate is {{ article.metafields.customsection.product }}

My current code to try to get the product object:

{%- assign productNames = article.metafields.customsection.product -%}
<ul>
  {%- for productSingle in productNames -%}
    {% assign productSingle = all_products[productSingle] %}
    <li>{{ productSingle }}</li>
  {%- endfor -%}
</ul>

 

I want to get the product object so I can do multiple things with it. Any help is greatly appreciated. Thank you.

0 Likes

This is an accepted solution.

Hi @redshot ,

if you metafield text, stores products handles comma separated, you can use this code to get the product object

 

{% assign productNames  = 'product-1-handle,product-2-handle' | split: "," %}
<ul>
	{%- for productName in productNames -%}
		{% assign productSingle = all_products[productName] %}
		<li>{{ productSingle.title }}</li>
	{%- endfor -%}
</ul

 

• If the problem is solved remember to click ✅ to Accept Solution
• Press Like ?? if the answer was helpful
• Shopify/Shopify Plus custom development, optimization and consulting. You can hire me here.
0 Likes
redshot
Tourist
12 2 2

Hello @drakedev ,

 

Thank you for your answer. I got it right now.

I now understand that {{ all_products }} is a global object that can be used to access products by their handles. The correct object for my situation was all_products[productSingle] .  I used it inside a loop and it worked.

 

{%- assign productNames = article.metafields.customsection.product -%}
<ul>
  {%- for productSingle in productNames -%}
    {% comment %}
      You can customize the code below to whatever code you want it to be,
     {% endcomment %}
    <li>all_products[productSingle].title</li>  
  {%- endfor -%}
</ul>

 

0 Likes