Multiply Metafield by quantity on Checkout Page

In the cart under the product description, I want to show the number stored in a metafield and then multiply the number by the quantity purchased. I am having problems displaying this metafield in the Cart on Dawn theme. I can display the metafield on a product page, but I can’t see the information in the cart.

Then, at the bottom of the page, I want to display the total of the metafield quantities (separately from the subtotal).

I have this but it isn’t returning anything. Maybe I don’t know where to put it?

{%- assign length_in = 0 -%}

{%- for item in cart.items -%}

{%- assign length_in = length_in | plus: product.metafields.custom.length_in | times: item.quantity -%}

{%- endfor -%}

Your variable and filter use is sound.

However you are referencing a non-existent product when you are in an cart.items loop that is referencing item (a line_item object). There is no product as it’s not on a product page or in a collection loop.

Use **item.**product.metafields.custom.length_in in your plus filter addition.

Read through the cart code to notice how when it needs a deeper product property it goes through the item object chain to the product object it’s based on.

To output your length_in use: {{ length_in }}

For additional info display in dawn you’d want to put it in the code near the line item properties area.

https://github.com/Shopify/dawn/blob/main/sections/main-cart-items.liquid#L133

Keeping in mind the higher display logic that may make that entire area not show:

.. if item.product.has_only_default_variant == false…

https://github.com/Shopify/dawn/blob/main/sections/main-cart-items.liquid#L119

1 Like

Thank you!

I am putting this code in the wrong place. The {{ length_in }} is calculating for the total quantity in my cart, not for the quantity of each item first.

I need it to do two things:

  • calculate by item quantity

  • add subtotal of all of those

Is that possible?

Thank you