Skip to content

Commit abc7dc8

Browse files
author
Stanislav Idolov
authored
ENGCOM-2704: Fix the issue with "Shipping address is not set" exception #16753
2 parents 48c9049 + 3c76541 commit abc7dc8

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

app/code/Magento/Multishipping/Controller/Checkout.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Customer\Api\AccountManagementInterface;
99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Framework\App\RequestInterface;
11+
use Magento\Framework\Exception\StateException;
1112

1213
/**
1314
* Multishipping checkout controller
@@ -84,6 +85,7 @@ protected function _getCheckoutSession()
8485
*
8586
* @param RequestInterface $request
8687
* @return \Magento\Framework\App\ResponseInterface
88+
* @throws \Magento\Framework\Exception\NotFoundException
8789
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8890
* @SuppressWarnings(PHPMD.NPathComplexity)
8991
*/
@@ -152,7 +154,15 @@ public function dispatch(RequestInterface $request)
152154
return parent::dispatch($request);
153155
}
154156

155-
$quote = $this->_getCheckout()->getQuote();
157+
try {
158+
$checkout = $this->_getCheckout();
159+
} catch (StateException $e) {
160+
$this->getResponse()->setRedirect($this->_getHelper()->getMSNewShippingUrl());
161+
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
162+
return parent::dispatch($request);
163+
}
164+
165+
$quote = $checkout->getQuote();
156166
if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
157167
$this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
158168
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);

app/code/Magento/Multishipping/Helper/Url.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function getMSShippingAddressSavedUrl()
6363
return $this->_getUrl('multishipping/checkout_address/shippingSaved');
6464
}
6565

66+
/**
67+
* Retrieve register url
68+
*
69+
* @return string
70+
*/
71+
public function getMSNewShippingUrl()
72+
{
73+
return $this->_getUrl('multishipping/checkout_address/newShipping');
74+
}
75+
6676
/**
6777
* Retrieve register url
6878
*

app/code/Magento/Quote/Model/ShippingMethodManagement.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Quote\Api\Data\AddressInterface;
1616
use Magento\Quote\Api\Data\EstimateAddressInterface;
1717
use Magento\Quote\Api\ShipmentEstimationInterface;
18+
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
1819

1920
/**
2021
* Shipping method read service
@@ -62,6 +63,11 @@ class ShippingMethodManagement implements
6263
*/
6364
private $addressFactory;
6465

66+
/**
67+
* @var QuoteAddressResource
68+
*/
69+
private $quoteAddressResource;
70+
6571
/**
6672
* Constructor
6773
*
@@ -70,20 +76,24 @@ class ShippingMethodManagement implements
7076
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
7177
* @param Quote\TotalsCollector $totalsCollector
7278
* @param AddressInterfaceFactory|null $addressFactory
79+
* @param QuoteAddressResource|null $quoteAddressResource
7380
*/
7481
public function __construct(
7582
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
7683
Cart\ShippingMethodConverter $converter,
7784
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
7885
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
79-
AddressInterfaceFactory $addressFactory = null
86+
AddressInterfaceFactory $addressFactory = null,
87+
QuoteAddressResource $quoteAddressResource = null
8088
) {
8189
$this->quoteRepository = $quoteRepository;
8290
$this->converter = $converter;
8391
$this->addressRepository = $addressRepository;
8492
$this->totalsCollector = $totalsCollector;
8593
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
8694
->get(AddressInterfaceFactory::class);
95+
$this->quoteAddressResource = $quoteAddressResource ?: ObjectManager::getInstance()
96+
->get(QuoteAddressResource::class);
8797
}
8898

8999
/**
@@ -170,9 +180,9 @@ public function set($cartId, $carrierCode, $methodCode)
170180
* @param string $methodCode The shipping method code.
171181
* @return void
172182
* @throws InputException The shipping method is not valid for an empty cart.
173-
* @throws CouldNotSaveException The shipping method could not be saved.
174183
* @throws NoSuchEntityException Cart contains only virtual products. Shipping method is not applicable.
175184
* @throws StateException The billing or shipping address is not set.
185+
* @throws \Exception
176186
*/
177187
public function apply($cartId, $carrierCode, $methodCode)
178188
{
@@ -188,6 +198,8 @@ public function apply($cartId, $carrierCode, $methodCode)
188198
}
189199
$shippingAddress = $quote->getShippingAddress();
190200
if (!$shippingAddress->getCountryId()) {
201+
// Remove empty quote address
202+
$this->quoteAddressResource->delete($shippingAddress);
191203
throw new StateException(__('Shipping address is not set'));
192204
}
193205
$shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode);

app/code/Magento/Quote/Test/Unit/Model/ShippingMethodManagementTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Magento\Quote\Model\Quote\Address\Rate;
1616
use Magento\Quote\Model\Quote\TotalsCollector;
1717
use Magento\Quote\Model\QuoteRepository;
18+
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
1819
use Magento\Quote\Model\ShippingMethodManagement;
20+
use Magento\Store\Model\Store;
1921
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2022

2123
/**
@@ -83,6 +85,16 @@ class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
8385
*/
8486
private $totalsCollector;
8587

88+
/**
89+
* @var Store|MockObject
90+
*/
91+
private $storeMock;
92+
93+
/**
94+
* @var QuoteAddressResource|MockObject
95+
*/
96+
private $quoteAddressResource;
97+
8698
protected function setUp()
8799
{
88100
$this->objectManager = new ObjectManager($this);
@@ -98,7 +110,8 @@ protected function setUp()
98110
$className = \Magento\Framework\Reflection\DataObjectProcessor::class;
99111
$this->dataProcessor = $this->createMock($className);
100112

101-
$this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
113+
$this->quoteAddressResource = $this->createMock(QuoteAddressResource::class);
114+
$this->storeMock = $this->createMock(Store::class);
102115
$this->quote = $this->getMockBuilder(Quote::class)
103116
->disableOriginalConstructor()
104117
->setMethods([
@@ -150,6 +163,7 @@ protected function setUp()
150163
'converter' => $this->converter,
151164
'totalsCollector' => $this->totalsCollector,
152165
'addressRepository' => $this->addressRepository,
166+
'quoteAddressResource' => $this->quoteAddressResource,
153167
]
154168
);
155169

@@ -362,6 +376,7 @@ public function testSetMethodWithoutShippingAddress()
362376
$this->quote->expects($this->once())
363377
->method('getShippingAddress')->will($this->returnValue($this->shippingAddress));
364378
$this->shippingAddress->expects($this->once())->method('getCountryId')->will($this->returnValue(null));
379+
$this->quoteAddressResource->expects($this->once())->method('delete')->with($this->shippingAddress);
365380

366381
$this->model->set($cartId, $carrierCode, $methodCode);
367382
}
@@ -421,6 +436,7 @@ public function testSetMethodWithoutAddress()
421436
->method('getShippingAddress')
422437
->willReturn($this->shippingAddress);
423438
$this->shippingAddress->expects($this->once())->method('getCountryId');
439+
$this->quoteAddressResource->expects($this->once())->method('delete')->with($this->shippingAddress);
424440

425441
$this->model->set($cartId, $carrierCode, $methodCode);
426442
}

0 commit comments

Comments
 (0)