Create Order Programmatically in Magento 2

How to Create Order Programmatically in Magento 2?

Today, we’re going to teach you guys how to create order programmatically in Magento 2.

Learning how to create order programmatically in Magento 2 is very helpful for store owners because the manual process is quite a time-consuming process.

However, in order to create order programmatically in Magento 2, we need to first create a quote.

Below are the steps you need to follow to create a quote and order programmatically in Magento 2.

Step-by-Step Process to Create Order Programmatically in Magento 2

First of all, we’ll use the following sample data to create both quote and order in Magento 2.

<?php

$order = [

    'currency_id' => 'USD',

    'email' => '[email protected]',

    'shipping_address' => ['firstname' => 'John',

        'lastname' => 'Green',

        'street' => 'xxxxxx',

        'city' => 'xxxxxxx',

        'country_id' => 'US',

        'region' => 'xxxxx',

        'postcode' => '85001',

        'telephone' => '52556542',

        'fax' => '3242322556',

        'save_in_address_book' => 1],

    'items' => [

        ['product_id' => '1', 'qty' => 1],

        ['product_id' => '2', 'qty' => 2]]

    ];

?>

After that, we now need to create a function in the module helper file as shown below:

<?php

namespaceYourNameSpace\ModuleName\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper

{

    public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product $product, \Magento\Framework\Data\Form\FormKey $formkey, \Magento\Quote\Model\QuoteFactory $quote, \Magento\Quote\Model\QuoteManagement $quoteManagement, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Sales\Model\Service\OrderService $orderService)

    {

        $this->storeManager = $storeManager;

        $this->product = $product;

        $this->formkey = $formkey;

        $this->quote = $quote;

        $this->quoteManagement = $quoteManagement;

        $this->customerFactory = $customerFactory;

        $this->customerRepository = $customerRepository;

        $this->orderService = $orderService;

        parent::__construct($context);

    }

    public function createOrder($order)

    {

        $store = $this->storeManager->getStore();

        $websiteId = $this->storeManager->getStore()->getWebsiteId();

        $customer = $this->customerFactory->create();

        $customer->setWebsiteId($websiteId);

        $customer->loadByEmail($order['email']); // load customet by email address

       if (!$customer->getEntityId()) {

            //If not avilable then create this customer

           $customer->setWebsiteId($websiteId)->setStore($store)->setFirstname($order['shipping_address']['firstname'])->setLastname($order['shipping_address']['lastname'])->setEmail($order['email'])->setPassword($order['email']);

            $customer->save();

        }

        $quote = $this->quote->create(); // Create Quote Object

       $quote->setStore($store); // Set Store

       $customer = $this->customerRepository->getById($customer->getEntityId());

        $quote->setCurrency();

        $quote->assignCustomer($customer); // Assign quote to Customer

        //add items in quote

       foreach ($order['items'] as $item) {

            $product = $this->product->load($item['product_id']);

            $product->setPrice($item['price']);

            $quote->addProduct($product, intval($item['qty']));

        }

        $quote->getBillingAddress()->addData($order['shipping_address']);

        $quote->getShippingAddress()->addData($order['shipping_address']);

        // Collect Rates and Set Shipping & Payment Method

       $shippingAddress = $quote->getShippingAddress();

        $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping');

        $quote->setPaymentMethod('checkmo');

        $quote->setInventoryProcessed(false);

        $quote->save();

        // Set Sales Order Payment

       $quote->getPayment()->importData(['method' => 'checkmo']);

        // Collect Totals & Save Quote

       $quote->collectTotals()->save();

        // Create Order From Quote

       $orderdata = $this->quoteManagement->submit($quote);

        $orderdata->setEmailSent(0);

        $increment_id = $order->getRealOrderId();

        if ($orderdata->getEntityId()) {

            $result['order_id'] = $orderdata->getRealOrderId();

        } else {

            $result = ['error' => 1, 'msg' => 'Your custom message'];

        }

        return $result;

    }

}

?>

And that’s about it!

Conclusion

This is all you need to do to create a quote & order programmatically in Magento 2.

And if you need our professional assistance, feel free to contact us at any time.

Magento 2 Development Services

Tags