Skip to content

Commit ee2f8a4

Browse files
committed
Merge remote-tracking branch 'origin/AC-428' into gl_pr_arrows_april21_2022
2 parents 621bc9d + 0a18062 commit ee2f8a4

File tree

5 files changed

+227
-101
lines changed

5 files changed

+227
-101
lines changed

app/code/Magento/Multishipping/Model/Cart/Controller/CartPlugin.php

Lines changed: 7 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,27 @@
88
namespace Magento\Multishipping\Model\Cart\Controller;
99

1010
use Magento\Checkout\Controller\Cart;
11-
use Magento\Checkout\Model\Session;
12-
use Magento\Customer\Api\AddressRepositoryInterface;
1311
use Magento\Framework\App\RequestInterface;
1412
use Magento\Framework\Exception\LocalizedException;
15-
use Magento\Multishipping\Model\DisableMultishipping;
16-
use Magento\Quote\Api\CartRepositoryInterface;
17-
use Magento\Quote\Model\Quote;
13+
use Magento\Multishipping\Model\Cart\MultishippingClearItemAddress;
1814

1915
/**
2016
* Cleans shipping addresses and item assignments after MultiShipping flow
2117
*/
2218
class CartPlugin
2319
{
2420
/**
25-
* @var CartRepositoryInterface
21+
* @var MultishippingClearItemAddress
2622
*/
27-
private $cartRepository;
23+
private $multishippingClearItemAddress;
2824

2925
/**
30-
* @var Session
31-
*/
32-
private $checkoutSession;
33-
34-
/**
35-
* @var AddressRepositoryInterface
36-
*/
37-
private $addressRepository;
38-
39-
/**
40-
* @var DisableMultishipping
41-
*/
42-
private $disableMultishipping;
43-
44-
/**
45-
* @param CartRepositoryInterface $cartRepository
46-
* @param Session $checkoutSession
47-
* @param AddressRepositoryInterface $addressRepository
48-
* @param DisableMultishipping $disableMultishipping
26+
* @param MultishippingClearItemAddress $multishippingClearItemAddress
4927
*/
5028
public function __construct(
51-
CartRepositoryInterface $cartRepository,
52-
Session $checkoutSession,
53-
AddressRepositoryInterface $addressRepository,
54-
DisableMultishipping $disableMultishipping
29+
MultishippingClearItemAddress $multishippingClearItemAddress
5530
) {
56-
$this->cartRepository = $cartRepository;
57-
$this->checkoutSession = $checkoutSession;
58-
$this->addressRepository = $addressRepository;
59-
$this->disableMultishipping = $disableMultishipping;
31+
$this->multishippingClearItemAddress = $multishippingClearItemAddress;
6032
}
6133

6234
/**
@@ -70,62 +42,6 @@ public function __construct(
7042
*/
7143
public function beforeDispatch(Cart $subject, RequestInterface $request)
7244
{
73-
/** @var Quote $quote */
74-
$quote = $this->checkoutSession->getQuote();
75-
$isMultipleShippingAddressesPresent = $quote->isMultipleShippingAddresses();
76-
if ($isMultipleShippingAddressesPresent || $this->isDisableMultishippingRequired($request, $quote)) {
77-
$this->disableMultishipping->execute($quote);
78-
foreach ($quote->getAllShippingAddresses() as $address) {
79-
$quote->removeAddress($address->getId());
80-
}
81-
82-
$shippingAddress = $quote->getShippingAddress();
83-
$defaultShipping = $quote->getCustomer()->getDefaultShipping();
84-
if ($defaultShipping) {
85-
$defaultCustomerAddress = $this->addressRepository->getById($defaultShipping);
86-
$shippingAddress->importCustomerAddressData($defaultCustomerAddress);
87-
}
88-
if ($isMultipleShippingAddressesPresent) {
89-
$this->checkoutSession->setMultiShippingAddressesFlag(true);
90-
}
91-
$this->cartRepository->save($quote);
92-
} elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) {
93-
$quote->setTotalsCollectedFlag(false);
94-
$this->cartRepository->save($quote);
95-
}
96-
}
97-
98-
/**
99-
* Checks whether quote has virtual items
100-
*
101-
* @param Quote $quote
102-
* @return bool
103-
*/
104-
private function isVirtualItemInQuote(Quote $quote): bool
105-
{
106-
$items = $quote->getItems();
107-
if (!empty($items)) {
108-
foreach ($items as $item) {
109-
if ($item->getIsVirtual()) {
110-
return true;
111-
}
112-
}
113-
}
114-
115-
return false;
116-
}
117-
118-
/**
119-
* Check if we have to disable multishipping mode depends on the request action name
120-
*
121-
* We should not disable multishipping mode if we are adding a new product item to the existing quote
122-
*
123-
* @param RequestInterface $request
124-
* @param Quote $quote
125-
* @return bool
126-
*/
127-
private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool
128-
{
129-
return $request->getActionName() !== "add" && $quote->getIsMultiShipping();
45+
$this->multishippingClearItemAddress->clearAddressItem($subject, $request);
13046
}
13147
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Multishipping\Model\Cart\Controller;
9+
10+
use Magento\Checkout\Controller\Sidebar\UpdateItemQty;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Multishipping\Model\Cart\MultishippingClearItemAddress;
14+
15+
/**
16+
* Cleans shipping addresses and item assignments after MultiShipping flow
17+
*/
18+
class MiniCartPlugin
19+
{
20+
/**
21+
* @var MultishippingClearItemAddress
22+
*/
23+
private $multishippingClearItemAddress;
24+
25+
/**
26+
* @param MultishippingClearItemAddress $multishippingClearItemAddress
27+
*/
28+
public function __construct(
29+
MultishippingClearItemAddress $multishippingClearItemAddress
30+
) {
31+
$this->multishippingClearItemAddress = $multishippingClearItemAddress;
32+
}
33+
34+
/**
35+
* Cleans shipping addresses and item assignments after MultiShipping flow
36+
*
37+
* @param UpdateItemQty $subject
38+
* @param RequestInterface $request
39+
* @return void
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
* @throws LocalizedException
42+
*/
43+
public function beforeDispatch(UpdateItemQty $subject, RequestInterface $request)
44+
{
45+
$this->multishippingClearItemAddress->clearAddressItem($subject, $request);
46+
}
47+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Multishipping\Model\Cart;
9+
10+
use Magento\Checkout\Controller\Cart;
11+
use Magento\Checkout\Controller\Sidebar\UpdateItemQty;
12+
use Magento\Checkout\Model\Session;
13+
use Magento\Checkout\Model\Cart as CartModel;
14+
use Magento\Customer\Api\AddressRepositoryInterface;
15+
use Magento\Framework\App\RequestInterface;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Multishipping\Model\DisableMultishipping;
18+
use Magento\Quote\Api\CartRepositoryInterface;
19+
use Magento\Quote\Model\Quote;
20+
21+
/**
22+
* Cleans shipping addresses and item assignments after MultiShipping flow
23+
*
24+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
25+
*/
26+
class MultishippingClearItemAddress
27+
{
28+
/**
29+
* @var CartRepositoryInterface
30+
*/
31+
private $cartRepository;
32+
33+
/**
34+
* @var Session
35+
*/
36+
private $checkoutSession;
37+
38+
/**
39+
* @var AddressRepositoryInterface
40+
*/
41+
private $addressRepository;
42+
43+
/**
44+
* @var DisableMultishipping
45+
*/
46+
private $disableMultishipping;
47+
48+
/**
49+
* @var CartModel
50+
*/
51+
private $cartmodel;
52+
53+
/**
54+
* @param CartRepositoryInterface $cartRepository
55+
* @param Session $checkoutSession
56+
* @param AddressRepositoryInterface $addressRepository
57+
* @param DisableMultishipping $disableMultishipping
58+
* @param CartModel $cartmodel
59+
*/
60+
public function __construct(
61+
CartRepositoryInterface $cartRepository,
62+
Session $checkoutSession,
63+
AddressRepositoryInterface $addressRepository,
64+
DisableMultishipping $disableMultishipping,
65+
CartModel $cartmodel
66+
) {
67+
$this->cartRepository = $cartRepository;
68+
$this->checkoutSession = $checkoutSession;
69+
$this->addressRepository = $addressRepository;
70+
$this->disableMultishipping = $disableMultishipping;
71+
$this->cartmodel = $cartmodel;
72+
}
73+
74+
/**
75+
* Cleans shipping addresses and item assignments after MultiShipping flow
76+
*
77+
* @param Cart|UpdateItemQty $subject
78+
* @param RequestInterface $request
79+
* @throws LocalizedException
80+
* @throws \Magento\Framework\Exception\NoSuchEntityException
81+
*/
82+
public function clearAddressItem($subject, $request)
83+
{
84+
/** @var Quote $quote */
85+
$quote = $this->checkoutSession->getQuote();
86+
$isMultipleShippingAddressesPresent = $quote->isMultipleShippingAddresses();
87+
if ($isMultipleShippingAddressesPresent || $this->isDisableMultishippingRequired($request, $quote)) {
88+
$this->disableMultishipping->execute($quote);
89+
foreach ($quote->getAllShippingAddresses() as $address) {
90+
$quote->removeAddress($address->getId());
91+
}
92+
93+
$shippingAddress = $quote->getShippingAddress();
94+
$defaultShipping = $quote->getCustomer()->getDefaultShipping();
95+
if ($defaultShipping) {
96+
$defaultCustomerAddress = $this->addressRepository->getById($defaultShipping);
97+
$shippingAddress->importCustomerAddressData($defaultCustomerAddress);
98+
}
99+
if ($isMultipleShippingAddressesPresent) {
100+
$this->checkoutSession->setMultiShippingAddressesFlag(true);
101+
}
102+
$this->cartRepository->save($quote);
103+
if ($subject instanceof UpdateItemQty) {
104+
$quote = $this->cartRepository->get($quote->getId());
105+
$this->cartmodel->setQuote($quote);
106+
}
107+
} elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) {
108+
$quote->setTotalsCollectedFlag(false);
109+
$this->cartRepository->save($quote);
110+
}
111+
}
112+
113+
/**
114+
* Checks whether quote has virtual items
115+
*
116+
* @param Quote $quote
117+
* @return bool
118+
*/
119+
private function isVirtualItemInQuote(Quote $quote): bool
120+
{
121+
$items = $quote->getItems();
122+
if (!empty($items)) {
123+
foreach ($items as $item) {
124+
if ($item->getIsVirtual()) {
125+
return true;
126+
}
127+
}
128+
}
129+
130+
return false;
131+
}
132+
133+
/**
134+
* Check if we have to disable multishipping mode depends on the request action name
135+
*
136+
* We should not disable multishipping mode if we are adding a new product item to the existing quote
137+
*
138+
* @param RequestInterface $request
139+
* @param Quote $quote
140+
* @return bool
141+
*/
142+
private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool
143+
{
144+
return $request->getActionName() !== "add" && $quote->getIsMultiShipping();
145+
}
146+
}

0 commit comments

Comments
 (0)