Solved

Calculating the sum of item variant metafields in the cart

TW90
Tourist
4 1 1

Hi everyone, I'm hoping someone can help me with this...

 

In our store, each product has a variant metafield called 'length_cm', where the value is a 'decimal'. This variant metafield is used to store the item's longest edge in cm's.

I am trying to write a liquid statement that calculates the sum of these lengths, specifically in the cart/checkout (we're on Shopify Plus).

e.g. 

Cart items:

Item 1variant.metafields.product_data.length_cm = 30.4

Item 2variant.metafields.product_data.length_cm = 45.1

Item 3variant.metafields.product_data.length_cm = 29.0

Sum of variant.metafields.product_data.length_cm = 104.5

 

I know I have to loop over the cart items for cart-related data, but I don't know how I can loop over the variant metafields of those items and add them all together.

 

I would appreciate some help if anyone has a solution?

Thanks!

 

Accepted Solutions (2)

LitCommerce
Astronaut
2860 684 732

This is an accepted solution.

Hi @TW90,

Please add code:

{%- assign length_cm = 0 -%}
{%- for item in cart.items -%}
    {%- assign length_cm = length_cm | plus: item.variant.metafields.product_data.length_cm -%}
{%- endfor -%}
Sum of variant.metafields.product_data.length_cm = {{ length_cm }}

Hope it helps!

 

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!

View solution in original post

TW90
Tourist
4 1 1

This is an accepted solution.

I figured this out...

 

{%- assign length_cm = 0 -%}
{%- for item in cart.items -%}
    {%- assign length_cm = length_cm | plus: item.variant.metafields.product_data.length_cm | times: item.quantity -%}
{%- endfor -%}

 

 

Note the addition of  'times: item.quantity' to the end. This now calculates the length times the line quantity.

Thanks, @LitCommerce for the help.

View solution in original post

Replies 7 (7)

LitCommerce
Astronaut
2860 684 732

This is an accepted solution.

Hi @TW90,

Please add code:

{%- assign length_cm = 0 -%}
{%- for item in cart.items -%}
    {%- assign length_cm = length_cm | plus: item.variant.metafields.product_data.length_cm -%}
{%- endfor -%}
Sum of variant.metafields.product_data.length_cm = {{ length_cm }}

Hope it helps!

 

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!
TW90
Tourist
4 1 1

Thank you @LitCommerce, this works perfectly and is exactly what I asked for.

Is there a way we can account for multiple quantities of the same line item? The code will only account for the single instance of the length metafield for that one line item, even though there might be 3 qty in the cart. The desired outcome would be that all line items, including their qty are added up.

e.g.

Cart contents:

Line item 1 - length_cm = 50 (qty 3)

Line item 2 - length_cm = 20 (qty 1)

The expected sum of length_cm = 170. However, the current code only calculates the sum as 70.

 

Hope you can help further. Thanks!

TW90
Tourist
4 1 1

This is an accepted solution.

I figured this out...

 

{%- assign length_cm = 0 -%}
{%- for item in cart.items -%}
    {%- assign length_cm = length_cm | plus: item.variant.metafields.product_data.length_cm | times: item.quantity -%}
{%- endfor -%}

 

 

Note the addition of  'times: item.quantity' to the end. This now calculates the length times the line quantity.

Thanks, @LitCommerce for the help.

LitCommerce
Astronaut
2860 684 732

Hi @TW90,

If you have any difficulty, you can contact me 🙂

Happy to help you.

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!
Grace_Amelia
Visitor
2 0 0

I'm trying to do something similar (we plant a certain amount of trees for each product purchased). Is this something I'd need to add to do differently to get this to display in the order notification email? Copying the code above and swapping in my metafield (just product level, not variant set) spits out 0 every time 😞 

 

TW90
Tourist
4 1 1

Hi @Grace_Amelia,

 

For the order confirmation, you'll need to loop over subtotal_line_items.

 

I believe this will work for you...

 

 

{%- assign lineTreesPlanted = 0 -%}
{%- assign lineQty = 0 -%}
{%- assign totalLineTreesPlanted = 0 -%}
{%- assign sumTreesPlanted = 0 -%}

{% for line in subtotal_line_items %}
{%- assign lineTreesPlanted = line.product.metafields.global.trees-planted (reference your metafield - I'm assuming it's just a number) -%}
{%- assign lineQty = line.quantity -%}
{%- assign totalLineTreesPlanted = lineTreesPlanted | times: lineQty -%}
{%- assign sumTreesPlanted = sumTreesPlanted | plus: totalLineTreesPlanted -%}
{%- endfor -%}

 

Where {{ sumTreesPlanted }} will output the total trees planted per order.

 

Grace_Amelia
Visitor
2 0 0

Thanks so much @TW90

 

I kept fumbling around in the dark and eventually landed on this which seemed to work too (don't ask how long it took me to get there)... 

 

{%- assign trees_planted = 0 -%}
{%- for line in order.line_items -%}
    {%- assign trees_planted = trees_planted | plus: line.product.metafields.NAMESPACE.KEY ​-%}
{%- endfor -%}

 

Hope that's helpful if anyone is looking to do similar.