Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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 1 - variant.metafields.product_data.length_cm = 30.4
Item 2 - variant.metafields.product_data.length_cm = 45.1
Item 3 - variant.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!
Solved! Go to the solution
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!
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.
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!
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!
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.
Hi @TW90,
If you have any difficulty, you can contact me 🙂
Happy to help you.
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 😞
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.
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.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024