Magento 2 place order in custom controller (difference between placeOrder and submit)
-
14-04-2021 - |
سؤال
In How to create order programmatically in Magento 2? it shows two methods on how to place a quote as an order. The first method uses
$order = $this->quoteManagement->submit($quote);
via \Magento\Quote\Model\QuoteManagement
. This returns an \Magento\Sales\Model\Order
object.
The second method is using
$cart = $this->cartRepositoryInterface->get($cart->getId());
$order_id = $this->cartManagementInterface->placeOrder($cart->getId());
This method uses both the \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface
and the \Magento\Quote\Api\CartManagementInterface $cartManagementInterface
.
Another approach I found is basically the first one, but using \Magento\Quote\Api\CartManagementInterface
instead...
What are the differences between those approaches?
المحلول
Calling $this->cartManagementInterface->placeOrder($cart->getId())
will still use $this->quoteManagement->submit($quote);
to create the order, only difference is that after the order is successfully created, the checkout session is updated with the quote and order details:
$this->checkoutSession->setLastQuoteId($quote->getId());
$this->checkoutSession->setLastSuccessQuoteId($quote->getId());
$this->checkoutSession->setLastOrderId($order->getId());
$this->checkoutSession->setLastRealOrderId($order->getIncrementId());
$this->checkoutSession->setLastOrderStatus($order->getStatus());
This is useful if you're placing your order from frontend since the success page is using the checkoutSession info for displaying.