Metafields in the cart not working

Solved
fabriziom
Tourist
6 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 

Jason
Shopify Expert
10360 158 2003

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 to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
fabriziom
Tourist
6 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 -%}
0 Likes