How to create a order from quote?

How to create a order from quote in magento 2?

Find full answer below.
We will load one product than we will create a quote for it and than we will convert that quote into an order.


<?php
use Magento\Framework\App\Action\Action;

class OrderCreateTest extends Action
{
    protected $_quoteFactory;
    protected $_orderModel;
    protected $_productModel;
    protected $_customerRepository;
    protected $_quoteManagementModel;
    
    public function __construct(\Magento\Quote\Model\QuoteFactory $quoteFactory, \Magento\Sales\Model\Order $orderModel, \Magento\Catalog\Model\Product $productModel, \Magento\Framework\App\Action\Context $context, \Magento\Quote\Model\QuoteManagement $quoteManagementModel, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository)
    {
        $this->_quoteFactory         = $quoteFactory;
        $this->_orderModel           = $orderModel;
        $this->_productModel         = $productModel;
        $this->_customerRepository   = $customerRepository;
        $this->_quoteManagementModel = $quoteManagementModel;
        
        parent::__construct($context);
    }
    
    public function execute()
    {
        try {
            $customerId = $storeId = 1;
            $customer   = $this->_customerRepository->getById($customerId);
            $quote      = $this->_quoteFactory->create();
            $product    = $this->_productModel->load(1);
            
            $quote->setStoreId($storeId);
            $quote->assignCustomer($customer);
            $quote->addProduct($product);
            $quote->setCustomerEmail('roni_cost@example.com');
            
            $addressData = array(
                "firstname" => "Test",
                "lastname" => "Test",
                "street" => "Sample Street 10",
                "city" => "Somewhere",
                "postcode" => "123456",
                "telephone" => "123456",
                "country_id" => "US",
                "region_id" => 12 // id from directory_country_region table
            );
            
            $billingAddress  = $quote->getBillingAddress()->addData($addressData);
            $shippingAddress = $quote->getShippingAddress()->addData($addressData);
            
            $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate')->setPaymentMethod('checkmo');
            
            $quote->getShippingAddress()->setShippingMethod('flatrate_flatrate');
            $quote->setPaymentMethod('checkmo');
            $quote->getPayment()->setQuote($quote);
            $quote->getPayment()->importData(array(
                'method' => 'checkmo'
            ));
            $quote->setInventoryProcessed(false);
            $quote->save();
            
            $order = $this->_quoteManagementModel->submit($quote);
            
            echo "<pre>ORDER DATA";
            echo $order->getId();
            exit;
            
            exit;
        }
        catch (\Exception $ex) {
            echo $ex->getMessage();
            exit;
        }
    }
}


If you like please share and subscribe.

Comments

Popular posts from this blog

Hosting and Goodies

Magento 2 Import Scripts

How to add product attribute value in minicart magento 2