Solved

Metafields in the cart not working

fabriziom
Tourist
7 1 1

Hi community,

i need to command the show/hide suggested product in cart from metafields (if it is already in cart), but it seems like i can't access metafields from cart.

the store is https://www.7amenfant.com with Prestige Theme

this is the simple code:

 

{%- assign in_cart = 0 -%}
...
{%- for item in cart.items -%} 
   {%- if item.products.metafields.global.cross_sell == 'example' -%} 
      {% increment in_cart %}
   {%- endif -%} 
{%- endfor -%}
...
{%- if in_cart == 0 -%} 
    {%- include 'CUSTOM_CODE' -%} 
{%- endif -%}

 

But the variable in_cart is always 0

 

thanks 

Accepted Solution (1)
fabriziom
Tourist
7 1 1

This is an accepted solution.

Hi jason, thanks

I solved in this way: 

{%- assign in_cart = true -%}
....
{%- for line_item in cart.items -%}
           {%- if line_item.variant.metafields.global.cross_sell == 'true' -%}
           {%- assign in_cart = false -%}
         {%- endif -%}
{%- endfor-%}
....
               {%- if in_cart == true -%}
                {%- include 'upsell' -%}
               {%- endif -%}

View solution in original post

Replies 2 (2)

Jason
Shopify Expert
11190 225 2283

It would not be:

item.products

But rather:

item.product

 

Try that minor adjustment and see if you get better results.

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
fabriziom
Tourist
7 1 1

This is an accepted solution.

Hi jason, thanks

I solved in this way: 

{%- assign in_cart = true -%}
....
{%- for line_item in cart.items -%}
           {%- if line_item.variant.metafields.global.cross_sell == 'true' -%}
           {%- assign in_cart = false -%}
         {%- endif -%}
{%- endfor-%}
....
               {%- if in_cart == true -%}
                {%- include 'upsell' -%}
               {%- endif -%}