Skip to content

Commit 7426d3d

Browse files
authored
Merge pull request #194 from magento-sparta/MDVA-126
[Support] Backport MAGETWO-49716 to 2.0.x branch
2 parents 91b9405 + 068f113 commit 7426d3d

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Multishipping\Model\Cart\Controller;
7+
8+
class CartPlugin
9+
{
10+
/**
11+
* @var \Magento\Quote\Api\CartRepositoryInterface
12+
*/
13+
private $cartRepository;
14+
15+
/**
16+
* @var \Magento\Checkout\Model\Session
17+
*/
18+
private $checkoutSession;
19+
20+
/**
21+
* @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository
22+
* @param \Magento\Checkout\Model\Session $checkoutSession
23+
*/
24+
public function __construct(
25+
\Magento\Quote\Api\CartRepositoryInterface $cartRepository,
26+
\Magento\Checkout\Model\Session $checkoutSession
27+
) {
28+
$this->cartRepository = $cartRepository;
29+
$this->checkoutSession = $checkoutSession;
30+
}
31+
32+
/**
33+
* @param \Magento\Checkout\Controller\Cart $subject
34+
* @param \Magento\Framework\App\RequestInterface $request
35+
* @return void
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
37+
*/
38+
public function beforeDispatch(
39+
\Magento\Checkout\Controller\Cart $subject,
40+
\Magento\Framework\App\RequestInterface $request
41+
) {
42+
/** @var \Magento\Quote\Model\Quote $quote */
43+
$quote = $this->checkoutSession->getQuote();
44+
45+
// Clear shipping addresses and item assignments after Multishipping flow
46+
if ($quote->isMultipleShippingAddresses()) {
47+
foreach ($quote->getAllShippingAddresses() as $address) {
48+
$quote->removeAddress($address->getId());
49+
}
50+
$quote->getShippingAddress();
51+
$quote->setIsMultiShipping(false);
52+
$quote->collectTotals();
53+
54+
$this->cartRepository->save($quote);
55+
}
56+
}
57+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Multishipping\Test\Unit\Model\Cart\Controller;
7+
8+
class CartPluginTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Multishipping\Model\Cart\Controller\CartPlugin
12+
*/
13+
private $model;
14+
15+
/**
16+
* @var \PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $cartRepositoryMock;
19+
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $checkoutSessionMock;
24+
25+
protected function setUp()
26+
{
27+
$this->cartRepositoryMock = $this->getMock('\Magento\Quote\Api\CartRepositoryInterface');
28+
$this->checkoutSessionMock = $this->getMock('\Magento\Checkout\Model\Session', [], [], '', false);
29+
$this->model = new \Magento\Multishipping\Model\Cart\Controller\CartPlugin(
30+
$this->cartRepositoryMock,
31+
$this->checkoutSessionMock
32+
);
33+
}
34+
35+
public function testBeforeDispatch()
36+
{
37+
$addressId = 100;
38+
$quoteMock = $this->getMock(
39+
'\Magento\Quote\Model\Quote',
40+
[
41+
'isMultipleShippingAddresses',
42+
'getAllShippingAddresses',
43+
'removeAddress',
44+
'getShippingAddress',
45+
'setIsMultiShipping',
46+
'collectTotals'
47+
],
48+
[],
49+
'',
50+
false
51+
);
52+
$this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
53+
54+
$addressMock = $this->getMock('\Magento\Quote\Model\Quote\Address', [], [], '', false);
55+
$addressMock->expects($this->once())->method('getId')->willReturn($addressId);
56+
57+
$quoteMock->expects($this->once())->method('isMultipleShippingAddresses')->willReturn(true);
58+
$quoteMock->expects($this->once())->method('getAllShippingAddresses')->willReturn([$addressMock]);
59+
$quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
60+
$quoteMock->expects($this->once())->method('getShippingAddress');
61+
$quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
62+
$quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf();
63+
64+
$this->cartRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
65+
66+
$this->model->beforeDispatch(
67+
$this->getMock('\Magento\Checkout\Controller\Cart', [], [], '', false),
68+
$this->getMock('\Magento\Framework\App\RequestInterface')
69+
);
70+
}
71+
}

app/code/Magento/Multishipping/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
<type name="Magento\Checkout\Model\Cart">
4343
<plugin name="multishipping_session_mapper" type="Magento\Multishipping\Model\Checkout\Type\Multishipping\Plugin" sortOrder="50" />
4444
</type>
45+
<type name="Magento\Checkout\Controller\Cart">
46+
<plugin name="multishipping_clear_addresses" type="Magento\Multishipping\Model\Cart\Controller\CartPlugin" />
47+
</type>
4548
</config>

0 commit comments

Comments
 (0)