Hi. I'm trying to create an order programatically in Magento 2
The main idea I want to get is the following:
To perform the last step I have created an observer to the checkout_cart_product_add_after method
In this event I do the following
// Get quote from the cart (_cart is a Magento\Checkout\Model\Cart) $quote = $this->_cart->getQuote (); // Ensure our quote has our customer ID (_current customer is Magento\Customer\Helper\Session\CurrentCustomer) $quote->setCustomerId ($this->_currentCustomer->getCustomerId ()); // Get the billing information and configure it $billing = $quote->getBillingAddress (); $billing->setFirstname ('lorem-ipsum'); // ... // Set the payment methods $quote->setPaymentMethod ('free'); // Set Sales Order Payment $quote->getPayment ()->importData (array ('method' => 'free')); // Configure quote $quote->setInventoryProcessed (false); $quote->collectTotals (); // Update changes $quote->save (); // Place order (Magento\Quote\Model\QuoteManagement) $order_id = $this->_quotemanagement->placeOrder ($quote->getId ());
At this point I have the following status
What If we be desierable is to have this order marked as paid on the backed and the customer receiving his email with his pursache's links but I can't figure out what I'm missing
Any help is welcome
Kind regards
Solved! Go to Solution.
Hi. I could fix the email problem using the Magento\Sales\Model\Order\Email\Sender\OrderSender to send the order email to the customer this way
$this->_orderSender->send ($order);