Latest news

Create Order Programmatically in Magento 2 (in 5 minutes)

No comments

In this Magento 2 tutorial, we will show you how to create order and quote programmatically in Magento 2 – in just 5 minutes.

Firstly, please applying the following data to create order and quote:

     'currency_id'  => 'USD',
     'email'        => '', //buyer email id
     'shipping_address' =>[
            'firstname'    => 'Tigren', //address Details
            'lastname'     => 'Solutions',
                    'street' => 'Broadway',
                    'city' => 'New York',
            'country_id' => 'US',
            'region' => 'Central',
            'postcode' => '33284',
            'telephone' => '02346234245',
            'fax' => '56456',
            'save_in_address_book' => 1
   'items'=> [ //array of product which order you want to create

Then, please write a function for creating the order in the module helper file.

    namespace Vendor\Namespace\Model\Subscription\Order;
    class Create
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Quote\Model\QuoteManagement $quoteManagement,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
            \Magento\Sales\Model\Service\OrderService $orderService,
            \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
            \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
            \Magento\Quote\Model\Quote\Address\Rate $shippingRate
        ) {
            $this->_storeManager = $storeManager;
            $this->_productFactory = $productFactory;
            $this->quoteManagement = $quoteManagement;
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;
            $this->orderService = $orderService;
            $this->cartRepositoryInterface = $cartRepositoryInterface;
            $this->cartManagementInterface = $cartManagementInterface;
            $this->shippingRate = $shippingRate;

         * @param array $orderData
         * @return int $orderId
        public function createOrder($orderData) {
            //init the store id and website id @todo pass from array
            $store = $this->_storeManager->getStore();
            $websiteId = $this->_storeManager->getStore()->getWebsiteId();
            //init the customer
            $customer->loadByEmail($orderData['email']);// load customet by email address
            //check the customer
                //If not available then create this customer
            //init the quote
            $cart_id = $this->cartManagementInterface->createEmptyCart();
            $cart = $this->cartRepositoryInterface->get($cart_id);
            // if you have already had the buyer id, you can load customer directly
            $customer= $this->customerRepository->getById($customer->getEntityId());
            $cart->assignCustomer($customer); //Assign quote to customer
            //add items in quote
            foreach($orderData['items'] as $item){
                $product = $this->_productFactory->create()->load($item['product_id']);
            //Set Address to quote @todo add section in order data for seperate billing and handle it
            // Collect Rates, Set Shipping & Payment Method
            $shippingAddress = $cart->getShippingAddress();
            //@todo set in order data
                ->setShippingMethod('flatrate_flatrate'); //shipping method
            $cart->setPaymentMethod('checkmo'); //payment method
            //@todo insert a variable to affect the invetory
            // Set sales order payment
            $cart->getPayment()->importData(['method' => 'checkmo']);
            // Collect total and save
            // Submit the quote and create the order
            $cart = $this->cartRepositoryInterface->get($cart->getId());
            $order_id = $this->cartManagementInterface->placeOrder($cart->getId());
            return $order_id;

By following the above instruction, you can easily create order and quote programmatically in your Magento 2 store.

If you have any problems when doing it, be free to ask us by leaving a comment below. See you in the next tutorial.

See More Tutorials:

How to Set Up Table Rates Shipping in Magento

How To Get Attribute ID, Name, Value From Attribute Code In Magento 2

How To Create Grouped Product in Magento 2

How To Create Newsletter Popup In Magento 2

How To Change Email Templates in Magento 2

1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 4.64 out of 5)
April LeeCreate Order Programmatically in Magento 2 (in 5 minutes)

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *