Skip to content

Commit 9868428

Browse files
committed
Fix the issue with "Shipping address is not set" exception, Fix the integrity constraint violation error when trying to access Shopping Cart
1 parent 7187c2e commit 9868428

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

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

+10-1
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
@@ -153,7 +154,15 @@ public function dispatch(RequestInterface $request)
153154
return parent::dispatch($request);
154155
}
155156

156-
$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();
157166
if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
158167
$this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
159168
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);

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

+10
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

+16-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Quote\Api\Data\AddressInterface;
1717
use Magento\Quote\Api\Data\EstimateAddressInterface;
1818
use Magento\Quote\Api\ShipmentEstimationInterface;
19+
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
1920

2021
/**
2122
* Shipping method read service
@@ -63,6 +64,11 @@ class ShippingMethodManagement implements
6364
*/
6465
private $addressFactory;
6566

67+
/**
68+
* @var QuoteAddressResource
69+
*/
70+
private $quoteAddressResource;
71+
6672
/**
6773
* Constructor
6874
*
@@ -71,20 +77,24 @@ class ShippingMethodManagement implements
7177
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
7278
* @param Quote\TotalsCollector $totalsCollector
7379
* @param AddressInterfaceFactory|null $addressFactory
80+
* @param QuoteAddressResource|null $quoteAddressResource
7481
*/
7582
public function __construct(
7683
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
7784
Cart\ShippingMethodConverter $converter,
7885
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
7986
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
80-
AddressInterfaceFactory $addressFactory = null
87+
AddressInterfaceFactory $addressFactory = null,
88+
QuoteAddressResource $quoteAddressResource = null
8189
) {
8290
$this->quoteRepository = $quoteRepository;
8391
$this->converter = $converter;
8492
$this->addressRepository = $addressRepository;
8593
$this->totalsCollector = $totalsCollector;
8694
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
8795
->get(AddressInterfaceFactory::class);
96+
$this->quoteAddressResource = $quoteAddressResource ?: ObjectManager::getInstance()
97+
->get(QuoteAddressResource::class);
8898
}
8999

90100
/**
@@ -172,6 +182,8 @@ public function set($cartId, $carrierCode, $methodCode)
172182
* @return void
173183
* @throws InputException The shipping method is not valid for an empty cart.
174184
* @throws NoSuchEntityException CThe Cart includes virtual product(s) only, so a shipping address is not used.
185+
* @throws StateException The billing or shipping address is not set.
186+
* @throws \Exception
175187
*/
176188
public function apply($cartId, $carrierCode, $methodCode)
177189
{
@@ -189,7 +201,9 @@ public function apply($cartId, $carrierCode, $methodCode)
189201
}
190202
$shippingAddress = $quote->getShippingAddress();
191203
if (!$shippingAddress->getCountryId()) {
192-
return;
204+
// Remove empty quote address
205+
$this->quoteAddressResource->delete($shippingAddress);
206+
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
193207
}
194208
$shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode);
195209
}

0 commit comments

Comments
 (0)