How to add metafield data on cart page?

How to add metafield data on cart page?

MCTP
New Member
5 0 0

Hi, I have created a Collection Metafield (single line with preset text to be selected). In my Sale collection aI have added this metafield and selected the text. Now how do I add this to the cart page in Impulse theme to display like the sample attached? So basically any products that are in the Sale collection I would like the red text to appear.Screenshot 2024-10-13 at 12.35.35 PM.pngsale not eligible.jpgScreenshot 2024-10-13 at 12.38.37 PM.png

Replies 6 (6)

PaulNewton
Shopify Partner
7450 656 1560

Hi @MCTP 👋  A collection metafield is available on collection templates, not directly on a product.

You have to go through all the line items > product > collections seeing if it's even in the relevant collection

https://shopify.dev/docs/api/liquid/objects/product#product-collections 

Then output the collections metafield https://shopify.dev/docs/api/liquid/objects/collections 

 

Put the following somewhere before the properties output.

Generally with the variants options titles output.

 

‌‌‌‌⚠💣 Always backup themes before changing files 💣⚠‌

{%- assign in_sale_collection = '' -%}
{%- for collection in line_item.product.collections -%}
 {%- if collection.handle == "collection-handle" -%}
  {%- assign in_sale_collection = collection -%}
 {%- endif -%}
{%- endfor -%}

{%- if in_sale_collection -%} {{ collection['collection-handle'].metafields.namespace.key }} {%- endif -%}

 Adjust namespace and key to need, and or use the .value accessor in some cases.

if the message is the same for all such items having this as metafield can be kinda moot /shrug

 

If your a merchant that need this customization implemented for you then contact me for services
Contact info in forum signature.
ALWAYS please provide context, examples: store url, theme name, post url(s) , or any further detail in ALL correspondence.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


MCTP
New Member
5 0 0

Do I need to use metafields? Could i just have code that if the compare price is being used than XX text will display?

PaulNewton
Shopify Partner
7450 656 1560

Depends on need, a metafield does make the text part of the collection admin UI instead of rooting around in theme code for a line that could be harder to auto translate if not done as a locale translation ,etc.

 

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


PaulNewton
Shopify Partner
7450 656 1560

Code exampoe has a typo, collection should be collections (plural, collections object accessor)

{{ collection['collection-handle'].metafields.namespace.key }}

should be

{{ collections['collection-handle'].metafields.namespace.key }}

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


MCTP
New Member
5 0 0

Thanks so much with helping me with this. I have done the following but it is not appearing. The mug is in the sale collection. I tried with .value and without.

Screenshot 2024-10-14 at 7.49.14 AM.pngScreenshot 2024-10-14 at 7.49.50 AM.pngScreenshot 2024-10-14 at 7.59.17 AM.pngScreenshot 2024-10-14 at 8.00.16 AM.png

PaulNewton
Shopify Partner
7450 656 1560

From the look of the code "line_item" isn't needed and something has already set the "product" as a variable to work with?

So it may be product.product.collections ... 👨‍💻 which would be the silly result of the theme developers not properly differentiating between a line-item and a product.

 

Context matters step back and find out what "line_item" is in the rest of the code it could be just "item", or "lineitem" or "product".

Then start outputting it's property values then work to the product : item.title, item.product.handle, item.product.collections, etc etc tectto be sure you've got the right variable and can get to the need contexts.

👨‍💻It can help to view the source(ctrl+u) of the page instead of solely relying on the cart page to reload and paint each time.

 

Good Luck.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org