Reading list Switch to dark mode

    Create Quote And Order Programmatically In Magento2

    Create Quote And Order Programmatically In Magento2

    Here we learn  how to create quote and order programmatically in  Magento2

    I’ll use following data for create quote and order

         'currency_id'  => 'USD',
         'email'        => '[email protected]', //buyer email id
         'shipping_address' =>[
    		    'firstname'	   => 'jhon', //address Details
    		    'lastname'	   => 'Deo',
                        'street' => 'xxxxx',
                        'city' => 'xxxxx',
    		    'country_id' => 'IN',
    		    'region' => 'xxx',
    		    'postcode' => '43244',
    		    'telephone' => '52332',
    		    'fax' => '32423',
    		    'save_in_address_book' => 1
       'items'=> [ //array of product which order you want to create

    I’ll write order create function in module helper file

    namespace YourNameSpace\ModuleName\Helper;
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
        * @param Magento\Framework\App\Helper\Context $context
        * @param Magento\Store\Model\StoreManagerInterface $storeManager
        * @param Magento\Catalog\Model\Product $product,
        * @param Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
        * @param Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
        * @param Magento\Customer\Model\CustomerFactory $customerFactory,
        * @param Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        * @param Magento\Sales\Model\Order $order
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\Product $product,
            \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
            \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
            \Magento\Sales\Model\Order $order
        ) {
            $this->_storeManager = $storeManager;
            $this->_product = $product;
            $this->cartRepositoryInterface = $cartRepositoryInterface;
            $this->cartManagementInterface = $cartManagementInterface;
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;
            $this->order = $order;
         * Create Order On Your Store
         * @param array $orderData
         * @return array
        public function createMageOrder($orderData) {
            $websiteId = $this->_storeManager->getStore()->getWebsiteId();
            $customer->loadByEmail($orderData['email']);// load customet by email address
            	//If not avilable then create this customer 
            $cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
            $quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote
          	// if you have allready buyer id then you can load customer directly 
            $customer= $this->customerRepository->getById($customer->getEntityId());
            $quote->assignCustomer($customer); //Assign quote to customer
            //add items in quote
            foreach($orderData['items'] as $item){
                $quote->addProduct($product, intval($item['qty']));
            //Set Address to quote
            // Collect Rates and Set Shipping & Payment Method
                            ->setShippingMethod('freeshipping_freeshipping'); //shipping method
            $quote->setPaymentMethod('checkmo'); //payment method
            $quote->setInventoryProcessed(false); //not effetc inventory
            // Set Sales Order Payment
            $quote->getPayment()->importData(['method' => 'checkmo']);
            $quote->save(); //Now Save quote and your quote is ready
            // Collect Totals
            // Create Order From Quote
            $quote = $this->cartRepositoryInterface->get($quote->getId());
            $orderId = $this->cartManagementInterface->placeOrder($quote->getId());
            $order = $this->order->load($orderId);
            $increment_id = $order->getRealOrderId();
                $result['order_id']= $order->getRealOrderId();
                $result=['error'=>1,'msg'=>'Your custom message'];
            return $result;

    Now using this method you can crate quote and order in magento2 programmatically.
    Thanks 🙂 .

    Searching for an experienced
    Magento Company ?
    Read More
    . . .
    Add a comment

    Leave a Comment

    Your email address will not be published.


  • vivek singh
    how to order products with custom options .
    • Webkul Support
      Hi Vivek,
      Thanks for your query, for sure we will update it in our future blogs.
  • Guhan Seenivasagam
    Orders were successfully imported. But the order items were not taken properly. It takes only the last item from item’s array. However quantity calculates properly. Why is it happening?
  • Rob Conings
    @disqus_dNfTZE7fUf:disqus I’m currently doing this for another customer. Are you intrested in outsourcing this ?
  • VijayaLakshmi M
    Where to insert this code and how to call, please show me some example.

    And i need an assistance in get shipping method process in Rest API in magento2.1.6
    If you are interest, pls let me .. and mail me

    thanks in advance

    – vijikannan

    • Webkul Support
      Thanks for the contact. For any requirement, you can contact us at [email protected]
  • Rustam Zinnatullin
    What is the use of `$quote->setInventoryProcessed(false)`?
    I guess `InventoryProcessed` is false by default, because inventory wasn’t processed. It’ll be processed in `submit` operation.
  • Ankit Shah
    Can we create order with product’s Manage Stock -> No?
  • Phuong LÊ
    L:65 $quote->assignCustomer($customer);
    This method required a MagentoCustomerApiDataCustomerInterface param.
    Here you give MagentoCustomerModelCustomer . It should not work imo.
  • Mohammad Mujassam
    tried the code but unable to add multiple items, instead its adding quantity for only one product.
    please suggest
  • Azher Uddin Farooqi

    In which table will I find the quote and order entry ?


  • Niteen Barde
    Not able to use “MagentoQuoteModelQuoteFactory $quote” as QuoteFactory is deprecated in magento 2.1. So what is an alternate option to use this?
    • Kirti Nariya
      Hi Niteen,
      I have found same issue. Did found any solution?


  • Shinichi
    How i can add the disscount?
  • poongudivanan
    HI i have tried this code, We found the issues in cart item.

    run the Order item with 2 products but it adding only one item added Quote and order.

    Kindly advice.

    • Mohammad Mujassam
      @poongudivanan:disqus did you get some solution for adding the multiple products
      • Kirti Nariya
        Hi Mohammad Mujassam,

        I have found same issue. Did found any solution?


  • Back to Top
    Great solution and the techs are very knowledgeable and helpful. Especially Nishi and Abhishek.
    Ann Beattie
    Senior Salesforce Consultant
    Talk to Sales


    Live Chat

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home