How to delete items from cart after applying discount codes?

How to delete items from cart after applying discount codes?

pbme
Shopify Partner
3 0 0

Hi,

 

I was wondering if there was a good way to remove an item from the cart when the total of the cart drops under a specific amount after adding discount codes. 

I found the property:

cart.line_items.delete_at(index)

and

cart.line_items.delete_if{}

 

These work but they don't actually remove the item. In my case when I remove the discount code, it auto adds that item back in.

Replies 5 (5)

gr_trading
Shopify Partner
2025 148 205

It's a complecated requirement, let me know if I understand your requirement correctly.

 

You have a threshold order amount which impacts when a copy or discount will be their in cart.

 

You want to check if amount after discount if less than the threshold a item will be removed.

 

Question is how you will decide which item to be removed?

 

And want that item back in cart if discount is removed.

 

Let me know if anything I missed.

For any custom development WhatsApp or connect at Email ID: support@grtrading.in for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
pbme
Shopify Partner
3 0 0

Not quite. So I already have a way to identify which line item to remove (line item property).

 

What I want to know is how to remove an item from the cart without it being added back. Currently, if my cart amount falls under a certain total (after applying discount), the item gets removed. My issue is that when the discount gets removed by the user, that removed item gets added back in automatically.

 

Not sure why that is happening if the cart.line_items.delete_if removes the item.

gr_trading
Shopify Partner
2025 148 205

Not sure about cart.line_items.delete_if working.

 

You can keep the variant id in cookie when your script updating the cart and add again to cart when discount code removed.

 

 

For any custom development WhatsApp or connect at Email ID: support@grtrading.in for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
pbme
Shopify Partner
3 0 0

So this is inside a Shopify Script, not sure I have access to cookies.

 

What I want to know is how to remove a line item using Shopify Scripts.

gr_trading
Shopify Partner
2025 148 205

Use this

 

jQuery.post('/cart/change.js', { quantity: 0, id: '37667537027267' })

 

Assuming you already have jQuery on page.

For any custom development WhatsApp or connect at Email ID: support@grtrading.in for quick consultation. | Shopify Free codes
To support Buy Me a Coffee