Converting Magento Quotes to Orders: A Comprehensive Guide
When working with the Magento e-commerce platform, you may find yourself needing to convert an existing quote into a real order. This process involves several steps, including setting the quote status to “submitted”, collecting the payment (if applicable), and finally converting the quote to an order.
Understanding Quotes in Magento
Before we dive into the conversion process, let’s briefly discuss what quotes are in the context of Magento 2. A quote is a temporary cart that represents a customerâs pending purchase. It contains items they have added but haven’t yet confirmed, which means it hasn’t been converted to an actual order.
Converting a Quote to an Order
Given your current setup, you already have a loaded quote object:
$quote = Mage::getModel('sales/quote')->load(100);
To convert this quote into an order and ensure the quote is also completed (i.e., no longer active), follow these steps:
Step 1: Set Quote to Submitted Status
First, you need to set the status of your quote to “submitted”. This signals that the customer has confirmed their cart.
$quote->setIsActive(false);
$quote->save();
Step 2: Create Sales Service Quote and Submit All
Next, create a sales/service_quote object using your loaded quote. Then call submitAll() to both convert the quote to an order and complete it simultaneously.
$service = Mage::getModel('sales/service_quote', $quote);
$order = $service->submitAndPlaceOrder();
The $order variable now contains the newly created order object.
Step 3: Invoicing the Order
By default, the order status will be “pending”. To change this to a processing state (i.e., invoiced), you can create an invoice and assign it to the order:
$invoice = Mage::getModel('sales/order_invoice');
$invoice->setOrder($order);
...
// Set other invoice parameters here
...
// Save the invoice, which will also update the order status
$invoice->save();
Step 4: Collect Payment (If Applicable)
Finally, if your order requires payment collection (e.g., not using a cash on delivery method), you would typically use Magento’s payment models to capture the funds:
// Assuming $payment is the relevant payment object
$payment->capture($amount);
Conclusion
Converting quotes to orders in Magento 2 involves multiple steps, from setting quote status to submitting all and creating invoices. The key is ensuring the submitAndPlaceOrder() call handles both the order conversion and the quote completion simultaneously.
Remember, this process assumes you are working with an active quote that requires no additional validation or business logic adjustments before submission.
For a more detailed walkthrough of Magento’s checkout workflow and how it relates to quotes and orders, I recommend checking out our Magento 2 Checkout Process Explained guide for further insights.