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.

5 REPLIES 5

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...

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

In Magento, the decision to reuse an existing quote vs creating a new one comes down to the session (or customer identity if logged in).

  • Guests: Magento stores the active quote ID in the session (checkout session). When the customer adds a product to the cart, Magento checks if there is already a quote linked to that session.
    • If yes → it reuses the existing quote so items accumulate in the same cart.
    • If no → it creates a new quote and associates it with that session.

  • Logged-in customers: Magento can also try to restore an active, non-converted quote from the database (so if they log back in, their cart persists).

So in your case (all customers are guests), the behavior you are seeing is Magento reusing the quote tied to the same session. A brand-new quote will only be created if:

  • The session expired or was cleared.

  • The previous quote was converted into an order (so no longer active).

  • You explicitly clear or replace the session quote in custom code.

For your coupon tracking:

  • Abandoned carts are simply quotes left in the "quote" table with is active = 1 and no order linked.

  • You could run that checks for quotes past a certain age (for example, 24 hours), see if they contain a coupon code, and then return the usage.

  • Alternatively, you might hook into events like sales quote remove item or sales quote delete before, or add your own cleanup logic to adjust coupon usage.

That way, you are not dependent on the customer actively removing the coupon — you handle abandoned quotes systematically.

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

When a visitor who is not logged in adds a product to the cart:

  1. Session & Quote Creation

    • Magento checks if the visitor has an existing quote (cart) in session.

    • If not, it creates a new quote record in quote table with customer_id = NULL.

    • The masked_quote_id is generated and stored in quote_id_mask (used for frontend GraphQL/REST security).

  2. Product Validation

    • Magento checks product availability, stock status, salable quantity, and required options.

    • It runs through product type logic (simple, configurable, bundle, etc.).

  3. Add Item to Quote

    • A new quote_item is created or updated (if the same product already exists).

    • Prices, discounts, and tax rules are applied.

  4. Session Storage

    • The quote ID is stored in PHP session / cookie (frontend cookie).

    • This allows the cart to persist during browsing.

  5. Cart Display

    • The mini cart or cart page fetches the quote data from session and displays items.

⚠️ Important: A guest’s cart exists only in the session. If the session expires or cookies are cleared, the cart is lost.


🔹 2. Customer (Logged-In) Add to Cart Process

When a logged-in customer adds an item:

  1. Quote Loading

    • Magento checks if there’s an active quote for that customer in the database (quote table, with customer_id).

    • If yes → load it. If not → create a new one.

  2. Product Validation

    • Same validation rules as guest: salable, stock, custom options, etc.

  3. Add Item to Quote

    • Insert/update row in quote_item table for that customer’s quote.

    • If adding same product, it increments qty instead of new row.

  4. Persistent Storage

    • Since the customer is logged in, the cart is tied to their customer_id in the DB.

    • Even if session ends or they log out and come back later → their cart persists.

  5. Synchronization

    • If a customer had items in their guest cart before logging in, Magento merges guest quote with their customer quote.

      You can check logic via below files:

      Magento\Checkout\Model\Cart "addProduct" function

      Magento\Checkout\Model\Session "getQuote" function