The following code is working fine to create order programmatically with simple products. How can I write the code to create order with configurable products?
if (file_exists($ orderFile) && file_exists($ orderItemFile)) { $ orderData = $ this->fileCsv->getData($ orderFile); $ orderItemData = $ this->fileCsv->getData($ orderItemFile); $ this->_resources = \Magento\Framework\App\ObjectManager::getInstance() ->get('Magento\Framework\App\ResourceConnection'); $ connection= $ this->_resources->getConnection(); $ tableName = $ this->_resources->getTableName('sequence_order_1'); foreach ($ orderData as $ ordValue) { if ($ ordValue[0] != "ORD_ID" && $ ordValue[2] != "ORD_VEC_ID") { $ orderData = [ 'currency_id' => 'USD', 'email' => $ ordValue[6], 'shipping_address' =>[ 'firstname' => $ ordValue[14], //address Details 'lastname' => $ ordValue[15], 'street' => $ ordValue[16], 'city' => $ ordValue[17], 'country_id' => $ ordValue[18], 'region_id' => $ ordValue[19], 'region' => $ ordValue[19], 'postcode' => $ ordValue[20], 'telephone' => $ ordValue[21], 'fax' => $ ordValue[22], 'save_in_address_book' => 1 ] ]; foreach ($ orderItemData as $ oriValue){ if ($ oriValue[0] != "ORI_ID" && $ oriValue[1] == $ ordValue[0]) { $ sku = $ oriValue[3]; $ _product = $ blockInstance->getProductBySku($ sku); $ productId = $ _product->getEntityId(); $ productItem[] = ['product_id'=>$ productId,'qty'=>$ oriValue[4],'price'=>$ oriValue[22]]; } } $ orderData['items'] = $ productItem; $ sql = "ALTER TABLE " . $ tableName . " AUTO_INCREMENT=".$ ordValue[0]; $ connection->query($ sql); $ 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(); } $ cart_id = $ this->cartManagementInterface->createEmptyCart(); $ cart = $ this->cartRepositoryInterface->get($ cart_id); $ cart->setStore($ store); $ customer= $ this->customerRepository->getById($ customer->getEntityId()); $ cart->setCurrency(); $ cart->assignCustomer($ customer); //Assign quote to customer //add items in quote foreach($ orderData['items'] as $ item){ $ product = $ this->_productFactory->create()->load($ item['product_id']); $ cart->addProduct( $ product, intval($ item['qty']) ); } //Set Address to quote @todo add section in order data for seperate billing and handle it $ cart->getBillingAddress()->addData($ orderData['shipping_address']); $ cart->getShippingAddress()->addData($ orderData['shipping_address']); $ shippingAddress = $ cart->getShippingAddress(); //@todo set in order data $ shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod('flatrate_flatrate'); //shipping method //$ cart->getShippingAddress()->addShippingRate($ this->rate); $ cart->setPaymentMethod('checkmo'); //payment method //@todo insert a variable to affect the invetory $ cart->setInventoryProcessed(false); // Set sales order payment $ cart->getPayment()->importData(['method' => 'checkmo']); // Collect total and saeve $ cart->collectTotals(); // Submit the quote and create the order $ cart->save(); $ cart = $ this->cartRepositoryInterface->get($ cart->getId()); $ orderId = $ this->cartManagementInterface->placeOrder($ cart->getId()); $ order = $ this->order->load($ orderId); $ shippingAmount = $ ordValue[27]; $ discountAmount = $ ordValue[34]; $ order->setCreatedAt($ ordValue[4]); $ order->setShippingAmount($ shippingAmount); $ order->setBaseShippingAmount($ shippingAmount); $ order->setDiscountAmount($ discountAmount); $ order->setBaseDiscountAmount($ discountAmount); $ order->setGrandTotal(($ order->getSubTotal()+$ shippingAmount)-$ discountAmount); $ order->save(); $ order->setEmailSent(0); if($ order->getEntityId()){ $ result['order_id']= $ order->getRealOrderId(); }else{ $ result=['error'=>1,'msg'=>'Your custom message']; } echo $ orderId; }