cancel
Showing results for 
Search instead for 
Did you mean: 

How Magento decides when to use an already created quote or create a new one?

How Magento decides when to use an already created quote or create a new one?

In my store all customers are guest, when a customer add a product to cart a quotes is created, but sometimes it uses an already created quote, so my question is: how Magento decides when to use an already created quote or create a new one?

This is becasue i changed the coupon logic, and i increment/decrement the coupon usage when the customer enters/removes the coupon in the first step of the cart (a plugin to Magento\Checkout\Controller\Cart\CouponPost) an i need to handle the scenario when a customer enters a coupon and leaves the cart abandoned, so i figuring how to look for this abandoned carts and if they have coupons to return the usage.

3 REPLIES 3

Re: How Magento decides when to use an already created quote or create a new one?

Hi @elunicotom528e ,

 

Magento decides based on the session. For guest customers, once a product is added to the cart, a quote is created and its ID is stored in the session. As long as that session is active, Magento will keep reusing the same quote. If the session expires or is cleared, a new quote is created.

For logged-in customers (it's not your case as you have all guest customers), Magento always tries to load their last active quote (is_active = 1) from the database; if none exists, it creates a new one.

In your case with coupons, abandoned carts can still hold applied coupons because Magento doesn’t release them automatically. If you’re adjusting coupon usage when the customer applies or removes one, you’ll need a cleanup job that looks for old active quotes with coupons and rolls back the usage.

 

Problem Solved? Accept as Solution!

 

Thanks,

Ankit

Ankit Jasani

Re: How Magento decides when to use an already created quote or create a new one?

Magento decides whether to reuse an existing quote or create a new one based on the customer’s session, login status, and store context. For guests, the system reuses the quote tied to the session ID until the session expires or the cart is cleared, at which point a new quote is generated when an item is added. For logged-in customers, Magento checks if there is an active quote linked to their customer ID in the current store; if found, it reuses it, otherwise a new one is created. Additionally, quotes are store-specific, so a customer can have separate active quotes for different stores or websites. When a customer logs in, Magento may also merge the existing session quote with their saved customer quote to ensure continuity.

Re: How Magento decides when to use an already created quote or create a new one?

That's exactly what i'm trying to do, i made a cron that looks for active quotes, with no reserved_order_id and if they have a coupon code to decrement its usage, the problem is that let's say my cart expires (after 30 minutes or so in our store) the quote is still active and "waiting" for another cart to me started, but also the quote that im using in my current purchase is also active, so if the cron runs when im making a purchase it could remove my coupon.

 

What i was looking at now is the session table and tygin to find in expired sessions data if there's any with a quote_id associated so in that case go to the quote and see if it has a coupon, but im not sure if this will work...