I am trying to create a free order with code, it all works up until adding a shipping address and method, it crashes with errors instead of creating the quote and i can’t see why as the code looks fine to me.
Billing address is no problem. I’m just going to provide a small section of the code relating to shipping address:
// add shipping address
/** @var Address $shippingAddress */
$shippingAddress = $this->_addressFactory->create();
$shippingAddress->addData([
'firstname' => (string)$order->Shipping->FirstName,
'middlename' => (string)$order->Shipping->MiddleName,
'lastname' => (string)$order->Shipping->LastName,
'street' => [(string)$order->Shipping->Street],
'city' => (string)$order->Shipping->City,
'postcode' => (string)$order->Shipping->PostCode,
'telephone' => (string)$order->Shipping->Telephone,
'country_id' => (string)$order->Shipping->Country,
'region' => (string)$order->Shipping->Region
]);
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping');
$quote->setShippingAddress($shippingAddress);
// add billing address
/** @var Address $billingAddress */
$billingAddress = $this->_addressFactory->create();
$billingAddress->addData([
'firstname' => (string)$order->Billing->FirstName,
'middlename' => (string)$order->Billing->MiddleName,
'lastname' => (string)$order->Billing->LastName,
'street' => [(string)$order->Billing->Street],
'city' => (string)$order->Billing->City,
'postcode' => (string)$order->Billing->PostCode,
'telephone' => (string)$order->Billing->Telephone,
'country_id' => (string)$order->Billing->Country,
'region' => (string)$order->Billing->Region
]);
$quote->setBillingAddress($billingAddress);
// collect all totals
$quote->collectTotals();
//$quote->getPayment()->setMethod('free');
// save the quote
$this->_cartRepo->save($quote);
this gives me an error:
Call to a member function getStoreId() on null#0
/vendor/magento/module-quote/Model/Quote/Address.php(1004):
MagentoQuoteModelQuoteAddress->requestShippingRates()
It seems to be this line in Address.php:
$storeId = $this->getQuote()->getStoreId() ?: $this->storeManager->getStore()->getId();