Skip to content

Commit

Permalink
Fix the issue with "Shipping address is not set" exception, Fix the i…
Browse files Browse the repository at this point in the history
…ntegrity constraint violation error when trying to access Shopping Cart
  • Loading branch information
dmytro-ch committed Aug 12, 2018
1 parent 7187c2e commit 9868428
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/code/Magento/Multishipping/Controller/Checkout.php
Expand Up @@ -8,6 +8,7 @@
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\StateException;

/**
* Multishipping checkout controller
Expand Down Expand Up @@ -153,7 +154,15 @@ public function dispatch(RequestInterface $request)
return parent::dispatch($request);
}

$quote = $this->_getCheckout()->getQuote();
try {
$checkout = $this->_getCheckout();
} catch (StateException $e) {
$this->getResponse()->setRedirect($this->_getHelper()->getMSNewShippingUrl());
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
return parent::dispatch($request);
}

$quote = $checkout->getQuote();
if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
$this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Multishipping/Helper/Url.php
Expand Up @@ -63,6 +63,16 @@ public function getMSShippingAddressSavedUrl()
return $this->_getUrl('multishipping/checkout_address/shippingSaved');
}

/**
* Retrieve register url
*
* @return string
*/
public function getMSNewShippingUrl()
{
return $this->_getUrl('multishipping/checkout_address/newShipping');
}

/**
* Retrieve register url
*
Expand Down
18 changes: 16 additions & 2 deletions app/code/Magento/Quote/Model/ShippingMethodManagement.php
Expand Up @@ -16,6 +16,7 @@
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\EstimateAddressInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;

/**
* Shipping method read service
Expand Down Expand Up @@ -63,6 +64,11 @@ class ShippingMethodManagement implements
*/
private $addressFactory;

/**
* @var QuoteAddressResource
*/
private $quoteAddressResource;

/**
* Constructor
*
Expand All @@ -71,20 +77,24 @@ class ShippingMethodManagement implements
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param Quote\TotalsCollector $totalsCollector
* @param AddressInterfaceFactory|null $addressFactory
* @param QuoteAddressResource|null $quoteAddressResource
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
Cart\ShippingMethodConverter $converter,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
AddressInterfaceFactory $addressFactory = null
AddressInterfaceFactory $addressFactory = null,
QuoteAddressResource $quoteAddressResource = null
) {
$this->quoteRepository = $quoteRepository;
$this->converter = $converter;
$this->addressRepository = $addressRepository;
$this->totalsCollector = $totalsCollector;
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
->get(AddressInterfaceFactory::class);
$this->quoteAddressResource = $quoteAddressResource ?: ObjectManager::getInstance()
->get(QuoteAddressResource::class);
}

/**
Expand Down Expand Up @@ -172,6 +182,8 @@ public function set($cartId, $carrierCode, $methodCode)
* @return void
* @throws InputException The shipping method is not valid for an empty cart.
* @throws NoSuchEntityException CThe Cart includes virtual product(s) only, so a shipping address is not used.
* @throws StateException The billing or shipping address is not set.
* @throws \Exception
*/
public function apply($cartId, $carrierCode, $methodCode)
{
Expand All @@ -189,7 +201,9 @@ public function apply($cartId, $carrierCode, $methodCode)
}
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
return;
// Remove empty quote address
$this->quoteAddressResource->delete($shippingAddress);
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
}
$shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode);
}
Expand Down

0 comments on commit 9868428

Please sign in to comment.