Create quote programmatically doesn't work

Create quote programmatically doesn't work

July 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create quote programmatically doesn't work

Understanding Quote Creation Issues in Magento

If your programmatically created quote works only when the sales_flat_quote table is empty, you’re likely encountering a common issue in Magento’s quote management system. Let me shed some light on this challenge and provide a solution.

The Problem with Non-Empty Quotes Table

When the sales_flat_quote table contains existing quotes, Magento expects certain conditions to be met before adding new items or saving the quote. If these conditions aren’t satisfied, you’ll face issues like your current scenario—success only when the table is empty.

The key reason lies in Magento’s multi-step quote creation process:

  1. Quote Assignment: A quote must first be assigned to a customer using assignCustomer()
  2. Store Association: The quote needs to associate with the correct store
  3. Item Addition: Items can only be added after the quote is created and associated with the customer and store
  4. Totals Collection: Magento recalculates totals (tax, discounts, etc.) before saving

Your current code attempts these steps but might fail if any existing quotes in the sales_flat_quote table have already been assigned to a customer or store.

The Correct Approach: Sequential Quote Creation

To ensure your quote creation works regardless of the sales_flat_quote status, follow this sequential approach:

  1. Clear Existing Cart: Before creating a new quote, clear any existing items from the current user’s cart
  2. Quote Assignment: Assign the customer to a fresh quote object (this clears any existing assignments)
  3. Item Addition: Add products directly to the assigned quote
  4. Totals Collection: Collect totals after adding all items
  5. Save and Redirect

Here is the revised code following these steps:

$customer_id = Mage::getSingleton('customer/session')->getId();
$quoteObj = Mage::getModel('sales/quote');
$quoteObj->setIsActive(1);
$quoteObj->assignCustomer(Mage::getModel('customer/customer')->load($customer_id));

// Clear existing items
$currentCartItems = $quoteObj->getAllVisibleItems();
foreach ($currentCartItems as $item) {
    $quoteObj->removeItem($item->getId());
}

$store_id = Mage::app()->getStore()->getId();
$productModel = Mage::getModel('catalog/product');
$product_id = $this->getRequest()->getParam('product_id');
$productObj = $productModel->load($product_id);

$quoteItem = Mage::getModel('sales/quote_item')
    ->setProduct($productObj)
    ->setQty(1)
    ->setStoreId($store_id);
$quoteObj->addItem($quoteItem);

$quoteObj->collectTotals();
$quoteObj->save();

$this->_redirect('checkout/cart/');

Conclusion

Ensuring your quote creation process is sequential—clearing existing items, assigning a customer, and properly collecting totals before saving—will resolve the “sales_flat_quote table must be empty” issue.

Remember to always test your code thoroughly in a staging environment before implementing it on a live site. If you’re looking for more advanced solutions or need expert Magento development services, feel free to explore our offerings at Laravel Company.

Happy coding!

Checkout Database Themes Customers

Our network