Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Add custom line item property in cart.

Add custom line item property in cart.

sniper2804
Shopify Partner
407 25 69

Hi,

 

I want to know if it's possible to add a custom line item property in cart based on a condition. 

Basically, I want to add the following {key: "Category", value: "Heavy"}, when the line item's price is more than $500.

What is the best way to do this? 

Automate & bulk assign products to shipping profile: Auto Shipping Profiles: Shipr
Calculate accurate shipping rates: Shipping Calculator: ShipMagic
Hide, rename, reorder payment methods at checkout: Payment Customization: PayMix
Replies 6 (6)

Ugurcan
Shopify Partner
348 7 34

Yes this is possible with javascript. You need to identify where the conditions can be found on the page and then pass them into a Javascript code, which will inject the attribute value into the cart form.

 

Pasilobus | eCommerce Experts, Shopify Apps & Development since 2015 -- www.pasilobus.com
sniper2804
Shopify Partner
407 25 69

Hi @Ugurcan ,

Thank you for your reply. Can you guide me to the correct file which I need to look at? I tried doing it in main-cart-items.liquid but it didn't work.

Automate & bulk assign products to shipping profile: Auto Shipping Profiles: Shipr
Calculate accurate shipping rates: Shipping Calculator: ShipMagic
Hide, rename, reorder payment methods at checkout: Payment Customization: PayMix
Ugurcan
Shopify Partner
348 7 34

It is different in every theme but you can start checking from parent file and follow partials from there. You can modify the code and add attributes or custom CSS classes to grab the values 

Pasilobus | eCommerce Experts, Shopify Apps & Development since 2015 -- www.pasilobus.com
sniper2804
Shopify Partner
407 25 69

Alright. Thank you. I will try to figure it out.

Automate & bulk assign products to shipping profile: Auto Shipping Profiles: Shipr
Calculate accurate shipping rates: Shipping Calculator: ShipMagic
Hide, rename, reorder payment methods at checkout: Payment Customization: PayMix

PaulNewton
Shopify Partner
7450 657 1565

Does the data need to specifically be on the line item, or can it be part of the cart.

If so use cart attributes ca

https://community.shopify.com/c/shopify-design/cart-use-cart-attributes-to-collect-more-information/... 

 

I mention that because to update a LIP after it's in the cart is a bit more complicated than adding an input to a product form. You will need to either use the ajax api ,or make sure it's submitted as part of the cart-to-checkout.

https://shopify.dev/docs/themes/ajax-api/reference/cart 

When using the ajax api make sure to preserve any existing LIP's on the line item, or you'll overwrite them (like tags on the rest api)

 

 

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


sniper2804
Shopify Partner
407 25 69

Hi @PaulNewton ,

Thanks for the insight. I think it will need to be Line Item Property as we want to use that as input to Shopify Functions.  I don't think Cart Attributes are present in the Shopify's function input query.
https://shopify.dev/docs/api/functions/reference/payment-customization/graphql/common-objects/cart

Automate & bulk assign products to shipping profile: Auto Shipping Profiles: Shipr
Calculate accurate shipping rates: Shipping Calculator: ShipMagic
Hide, rename, reorder payment methods at checkout: Payment Customization: PayMix