Here , I am going to explain that manage inventory in magento 2 during create order programmatically. There is already configuration setting in magento2 where you can set that inventory manage at the time of order creation or not.
But this works for all orders which you create manually or programatically. Sometimes we want to manage inventory for specific condition.
I am not explaining here that how to create order programmatically in magento2 .You can check this blog . It will help to you create order.
After follow all steps which is mentioned in attached blog.I hope your order successfully created.
Now add code for manage inventory.
You can check YourNameSpace\ModuleName\Helper\Data.php we already added code for manage but in this case, Magento manage inventory and deduct product quantity which you ordered.
$quote->setInventoryProcessed(false); $itemsData = $this–>productQty->getProductQty($quote->getAllItems()); $itemsforReindex = $this–>stockManagement->registerProductsSale($itemsData, $quote->getStore()–>getWebsiteId()); $this–>itemsForReindex->setItems($itemsforReindex);
<?php 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, \Magento\CatalogInventory\Observer\ItemsForReindex $itemsForReindex, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\CatalogInventory\Observer\ProductQty $productQty, \Magento\CatalogInventory\Api\StockManagementInterface $stockManagement ) { $this->_storeManager = $storeManager; $this->_product = $product; $this->cartRepositoryInterface = $cartRepositoryInterface; $this->cartManagementInterface = $cartManagementInterface; $this->customerFactory = $customerFactory; $this->customerRepository = $customerRepository; $this->order = $order; $this->itemsForReindex = $itemsForReindex; $this->stockRegistry = $stockRegistry; $this->productQty = $productQty; $this->stockManagement = $stockManagement; parent::__construct($context); } /** * Create Order On Your Store * * @param array $orderData * @return array * */ public function createMageOrder($orderData) { $store=$this->_storeManager->getStore(); $websiteId = $this->_storeManager->getStore()->getWebsiteId(); $customer=$this->customerFactory->create(); $customer->setWebsiteId($websiteId); $customer->loadByEmail($orderData['email']);// load customet by email address if(!$customer->getEntityId()){ //If not avilable then create this customer $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($orderData['shipping_address']['firstname']) ->setLastname($orderData['shipping_address']['lastname']) ->setEmail($orderData['email']) ->setPassword($orderData['email']); $customer->save(); } $cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart $quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote $quote->setStore($store); // if you have allready buyer id then you can load customer directly $customer= $this->customerRepository->getById($customer->getEntityId()); $quote->setCurrency(); $quote->assignCustomer($customer); //Assign quote to customer //add items in quote foreach($orderData['items'] as $item){ $product=$this->_product->load($item['product_id']); $product->setPrice($item['price']); $quote->addProduct($product, intval($item['qty'])); } //Set Address to quote $quote->getBillingAddress()->addData($orderData['shipping_address']); $quote->getShippingAddress()->addData($orderData['shipping_address']); // Collect Rates and Set Shipping & Payment Method $shippingAddress=$quote->getShippingAddress(); $shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod('freeshipping_freeshipping'); //shipping method $quote->setPaymentMethod('checkmo'); //payment method //not effetc inventory $quote->setInventoryProcessed(false); $itemsData = $this->productQty->getProductQty($quote->getAllItems()); $itemsforReindex = $this->stockManagement->registerProductsSale($itemsData, $quote->getStore()->getWebsiteId()); $this->itemsForReindex->setItems($itemsforReindex); // Set Sales Order Payment $quote->getPayment()->importData(['method' => 'checkmo']); $quote->save(); //Now Save quote and your quote is ready // Collect Totals $quote->collectTotals(); // Create Order From Quote $quote = $this->cartRepositoryInterface->get($quote->getId()); $orderId = $this->cartManagementInterface->placeOrder($quote->getId()); $order = $this->order->load($orderId); $order->setEmailSent(0); $increment_id = $order->getRealOrderId(); if($order->getEntityId()){ $result['order_id']= $order->getRealOrderId(); }else{ $result=['error'=>1,'msg'=>'Your custom message']; } return $result; } } ?>
If you want to that product quantity will not update after the order then add the following code.
$quote->setInventoryProcessed(true); $itemsData = $this->productQty->getProductQty($quote->getAllItems()); $this->stockManagement->revertProductsSale($itemsData, $quote->getStore()->getWebsiteId());
I hope it will help you. Thanks for reading this. If you get any issue then you can comment below.
Be the first to comment.