I tried to create order programmatically with flat rate shipping method with the help of below code. Here, order created successfully but shipping method and rates not apply to order.
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore(5);
$storeCode = $store->getCode();
$storeManager->setCurrentStore($storeCode);
$coreSession = $objectManager->create('\Magento\Framework\Session\SessionManagerInterface');
$coreSession->setIsFailOrder('YES'); // Biggest patch because we have to create order with archive status and also follow PCI compliance.
$orderData=[
'currency_id' => 'USD',
'email' => '[email protected]', //buyer email id
'shipping_address' =>[
'firstname' => 'John', //address Details
'lastname' => 'Doe',
'street' => '123 Demo',
'city' => 'Mageplaza',
'country_id' => 'US',
'region' => 43, //'xxx',
'postcode' => '10019',
'telephone' => '0123456789',
'fax' => '32423',
'save_in_address_book' => 1
],
'items'=> [ //array of product which order you want to create
['product_id' => '5', 'qty'=> 2]
]
];
$store = $storeManager->getStore();
$websiteId = $storeManager->getStore()->getWebsiteId();
$customer = $objectManager->create('Magento\Customer\Model\Customer');
$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();
}
$quote = $objectManager->create('Magento\Quote\Model\Quote');
$quote->setStore($store); //set store for which you create quote
// if you have allready buyer id then you can load customer directly
$customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById($customer->getEntityId());
$quote->assignCustomer($customer); //Assign quote to customer
//add items in quote
$product = $objectManager->create('Magento\Catalog\Model\Product');
foreach($orderData['items'] as $item){
$product = $product->load($item['product_id']);
$product->setPrice($product->getPrice());
$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
$shippingRate = $objectManager->get('Magento\Quote\Model\Quote\Address\Rate');
$shippingRate->setCode('flatrate_flatrate')->getPrice(0);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quote->getShippingAddress()->addShippingRate($shippingRate);
$shoppingCartPriceRule = $objectManager->create('Magento\SalesRule\Model\Rule');
$rule = $shoppingCartPriceRule->load(9);
$rule->setDiscountAmount('7')->save();
$quote->setCouponCode('amazoncode');
$quote->setPaymentMethod('cashondelivery'); //payment method
$quote->setInventoryProcessed(false); //not effetc inventory
$quote->save(); //Now Save quote and your quote is ready
// Set Sales Order Payment
$quote->getPayment()->importData(['method' => 'cashondelivery']);
// Collect Totals & Save Quote
$quote->collectTotals()->save();
// Create Order From Quote
$quoteManagement = $objectManager->create('Magento\Quote\Model\QuoteManagement');
$order = $quoteManagement->submit($quote);
$order->setEmailSent(0);
$payment = $order->getPayment();
$payment->setMethod('authorizenet_acceptjs'); // Assuming 'test' is updated payment method
$payment->save();
$order->setState('new');
$order->setStatus('archive');
$order->setEmailSent(0);
$order->save();
$incrementId = $order->getRealOrderId();
$custoemrName = $order->getCustomerName();
$coreSession->unsIsFailOrder();
echo $incrementId;exit;
Please help me how to assign shipping method and rates to order?
Regards Hiren Shah