We will define prepare our code in helper. In that function, we have to pass parameters for order creation.
First see parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$quoteParams = [ 'currency_id' => 'INR', 'email' => 'coffeewithmagento@gmail.com', 'shipping_address' => [ 'firstname' => 'Coffee', 'lastname' => 'WithMagento', 'street' => 'Demo Address', 'city' => 'Order City', 'country_id' => 'IN', 'region' => 'abc', 'postcode' => '123456', 'telephone' => '111111111', 'fax' => '12345', 'save_in_address_book' => 1 ], 'items'=> [ ['product_id'=> '10','qty' => 3], ['product_id'=> '11','qty' => 1] ] ]; |
We create a helper file and define function inside for an order creation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?php namespace CWM\Order\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { /** * @param Magento\Framework\App\Helper\Context $context * @param Magento\Store\Model\StoreManagerInterface $storeManagerInterface * @param Magento\Catalog\Model\Product $productModel * @param Magento\Framework\Data\Form\FormKey $formKey * @param Magento\Customer\Model\CustomerFactory $customerFactory * @param Magento\Quote\Model\Quote $quoteFactory * @param Magento\Quote\Model\QuoteManagement $quoteManagement * @param MagentoMagento\Customer\Api\CustomerRepositoryInterface $customerRepository * @param Magento\Sales\Model\Service\OrderService $orderService */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManagerInterface, \Magento\Catalog\Model\Product $productModel, \Magento\Framework\Data\Form\FormKey $formkey, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Quote\Model\QuoteFactory $quoteFactory, \Magento\Quote\Model\QuoteManagement $quoteManagement, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Sales\Model\Service\OrderService $orderService ) { $this->storeManagerInterface = $storeManagerInterface; $this->productModel = $productModel; $this->formkey = $formkey; $this->quoteFactory = $quoteFactory; $this->quoteManagement = $quoteManagement; $this->customerFactory = $customerFactory; $this->customerRepository = $customerRepository; $this->orderService = $orderService; parent::__construct($context); } /** * Create Order On Your Store * @param array $orderData * @return array * */ public function CreateCustomOrder($orderData) { $store=$this->storeManagerInterface->getStore(); $websiteId = $this->storeManagerInterface->getStore()->getWebsiteId(); $customer=$this->customerFactory->create(); $customer->setWebsiteId($websiteId); $customer->loadByEmail($orderData['email']); if(!$customer->getEntityId()){ $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($orderData['shipping_address']['firstname']) ->setLastname($orderData['shipping_address']['lastname']) ->setEmail($orderData['email']) ->setPassword($orderData['email']); $customer->save(); } $quote = $this->quoteFactory->create(); $quote->setStore($store); $customer= $this->customerRepository->getById($customer->getEntityId()); $quote->setCurrency(); $quote->assignCustomer($customer); // items code foreach($orderData['items'] as $item){ $product = $this->productModel->load($item['product_id']); $product->setPrice($item['price']); $quote->addProduct( $product, intval($item['qty']) ); } // Setting Address to quote $quote->getBillingAddress()->addData($orderData['shipping_address']); $quote->getShippingAddress()->addData($orderData['shipping_address']); // Collecting Rates and Set Shipping & Payment Method $shippingAddress = $quote->getShippingAddress(); $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping'); $quote->setPaymentMethod('checkmo'); $quote->setInventoryProcessed(false); $quote->save(); // Setting Sales Order Payment $quote->getPayment()->importData(['method' => 'checkmo']); // Collecting Totals & Save Quote $quote->collectTotals()->save(); // Creating Order From Quote $order = $this->quoteManagement->submit($quote); $order->setEmailSent(0); $increment_id = $order->getRealOrderId(); if($order->getEntityId()){ $result['order_id'] = $order->getRealOrderId(); } else{ $result = ['error'=> true, 'msg'=> 'Something went wrong']; } return $result; } } |