Skip to content

[Forwardport] Fix the issue with "Shipping address is not set" exception #16784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/code/Magento/Multishipping/Controller/Checkout.php
Original file line number Diff line number Diff line change
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 @@ -84,6 +85,7 @@ protected function _getCheckoutSession()
*
* @param RequestInterface $request
* @return \Magento\Framework\App\ResponseInterface
* @throws \Magento\Framework\Exception\NotFoundException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
Expand Down Expand Up @@ -152,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
Original file line number Diff line number Diff line change
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: 15 additions & 3 deletions app/code/Magento/Quote/Model/ShippingMethodManagement.php
Original file line number Diff line number Diff line change
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 @@ -171,9 +181,9 @@ public function set($cartId, $carrierCode, $methodCode)
* @param string $methodCode The shipping method code.
* @return void
* @throws InputException The shipping method is not valid for an empty cart.
* @throws CouldNotSaveException The shipping method could not be saved.
* @throws NoSuchEntityException CThe Cart includes virtual product(s) only, so a shipping address is not used.
* @throws StateException The billing or shipping address is missing. Set the address and try again.
* @throws StateException The billing or shipping address is not set.
* @throws \Exception
*/
public function apply($cartId, $carrierCode, $methodCode)
{
Expand All @@ -191,6 +201,8 @@ public function apply($cartId, $carrierCode, $methodCode)
}
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Quote\Model\Quote\TotalsCollector;
use Magento\Quote\Model\QuoteRepository;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
use Magento\Quote\Model\ShippingMethodManagement;
use Magento\Store\Model\Store;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
Expand Down Expand Up @@ -83,6 +85,16 @@ class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
*/
private $totalsCollector;

/**
* @var Store|MockObject
*/
private $storeMock;

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

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
Expand All @@ -98,7 +110,8 @@ protected function setUp()
$className = \Magento\Framework\Reflection\DataObjectProcessor::class;
$this->dataProcessor = $this->createMock($className);

$this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
$this->quoteAddressResource = $this->createMock(QuoteAddressResource::class);
$this->storeMock = $this->createMock(Store::class);
$this->quote = $this->getMockBuilder(Quote::class)
->disableOriginalConstructor()
->setMethods([
Expand Down Expand Up @@ -150,6 +163,7 @@ protected function setUp()
'converter' => $this->converter,
'totalsCollector' => $this->totalsCollector,
'addressRepository' => $this->addressRepository,
'quoteAddressResource' => $this->quoteAddressResource,
]
);

Expand Down Expand Up @@ -362,6 +376,7 @@ public function testSetMethodWithoutShippingAddress()
$this->quote->expects($this->once())
->method('getShippingAddress')->will($this->returnValue($this->shippingAddress));
$this->shippingAddress->expects($this->once())->method('getCountryId')->will($this->returnValue(null));
$this->quoteAddressResource->expects($this->once())->method('delete')->with($this->shippingAddress);

$this->model->set($cartId, $carrierCode, $methodCode);
}
Expand Down Expand Up @@ -421,6 +436,7 @@ public function testSetMethodWithoutAddress()
->method('getShippingAddress')
->willReturn($this->shippingAddress);
$this->shippingAddress->expects($this->once())->method('getCountryId');
$this->quoteAddressResource->expects($this->once())->method('delete')->with($this->shippingAddress);

$this->model->set($cartId, $carrierCode, $methodCode);
}
Expand Down