Skip to content

Commit 5d8dc6f

Browse files
committed
changes after static test failures
1 parent cc1b18e commit 5d8dc6f

File tree

5 files changed

+159
-335
lines changed

5 files changed

+159
-335
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
}

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

Lines changed: 7 additions & 92 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\Sidebar\UpdateItemQty;
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 MiniCartPlugin
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,63 +42,6 @@ public function __construct(
7042
*/
7143
public function beforeDispatch(UpdateItemQty $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-
$quote->setTotalsCollectedFlag(false);
92-
$this->cartRepository->save($quote);
93-
} elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) {
94-
$quote->setTotalsCollectedFlag(false);
95-
$this->cartRepository->save($quote);
96-
}
97-
}
98-
99-
/**
100-
* Checks whether quote has virtual items
101-
*
102-
* @param Quote $quote
103-
* @return bool
104-
*/
105-
private function isVirtualItemInQuote(Quote $quote): bool
106-
{
107-
$items = $quote->getItems();
108-
if (!empty($items)) {
109-
foreach ($items as $item) {
110-
if ($item->getIsVirtual()) {
111-
return true;
112-
}
113-
}
114-
}
115-
116-
return false;
117-
}
118-
119-
/**
120-
* Check if we have to disable multishipping mode depends on the request action name
121-
*
122-
* We should not disable multishipping mode if we are adding a new product item to the existing quote
123-
*
124-
* @param RequestInterface $request
125-
* @param Quote $quote
126-
* @return bool
127-
*/
128-
private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool
129-
{
130-
return $request->getActionName() !== "add" && $quote->getIsMultiShipping();
45+
$this->multishippingClearItemAddress->clearAddressItem($subject, $request);
13146
}
13247
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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\Customer\Api\AddressRepositoryInterface;
14+
use Magento\Framework\App\RequestInterface;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Multishipping\Model\DisableMultishipping;
17+
use Magento\Quote\Api\CartRepositoryInterface;
18+
use Magento\Quote\Model\Quote;
19+
20+
/**
21+
* Cleans shipping addresses and item assignments after MultiShipping flow
22+
*/
23+
class MultishippingClearItemAddress
24+
{
25+
/**
26+
* @var CartRepositoryInterface
27+
*/
28+
private $cartRepository;
29+
30+
/**
31+
* @var Session
32+
*/
33+
private $checkoutSession;
34+
35+
/**
36+
* @var AddressRepositoryInterface
37+
*/
38+
private $addressRepository;
39+
40+
/**
41+
* @var DisableMultishipping
42+
*/
43+
private $disableMultishipping;
44+
45+
/**
46+
* @param CartRepositoryInterface $cartRepository
47+
* @param Session $checkoutSession
48+
* @param AddressRepositoryInterface $addressRepository
49+
* @param DisableMultishipping $disableMultishipping
50+
*/
51+
public function __construct(
52+
CartRepositoryInterface $cartRepository,
53+
Session $checkoutSession,
54+
AddressRepositoryInterface $addressRepository,
55+
DisableMultishipping $disableMultishipping
56+
) {
57+
$this->cartRepository = $cartRepository;
58+
$this->checkoutSession = $checkoutSession;
59+
$this->addressRepository = $addressRepository;
60+
$this->disableMultishipping = $disableMultishipping;
61+
}
62+
63+
/**
64+
* Cleans shipping addresses and item assignments after MultiShipping flow
65+
*
66+
* @param Cart|UpdateItemQty $subject
67+
* @param RequestInterface $request
68+
* @throws LocalizedException
69+
* @throws \Magento\Framework\Exception\NoSuchEntityException
70+
*/
71+
public function clearAddressItem($subject, $request)
72+
{
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+
if ($subject instanceof UpdateItemQty) {
93+
$quote = $this->checkoutSession->getQuote();
94+
$quote->setTotalsCollectedFlag(false);
95+
$this->cartRepository->save($quote);
96+
}
97+
} elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) {
98+
$quote->setTotalsCollectedFlag(false);
99+
$this->cartRepository->save($quote);
100+
}
101+
}
102+
103+
/**
104+
* Checks whether quote has virtual items
105+
*
106+
* @param Quote $quote
107+
* @return bool
108+
*/
109+
private function isVirtualItemInQuote(Quote $quote): bool
110+
{
111+
$items = $quote->getItems();
112+
if (!empty($items)) {
113+
foreach ($items as $item) {
114+
if ($item->getIsVirtual()) {
115+
return true;
116+
}
117+
}
118+
}
119+
120+
return false;
121+
}
122+
123+
/**
124+
* Check if we have to disable multishipping mode depends on the request action name
125+
*
126+
* We should not disable multishipping mode if we are adding a new product item to the existing quote
127+
*
128+
* @param RequestInterface $request
129+
* @param Quote $quote
130+
* @return bool
131+
*/
132+
private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool
133+
{
134+
return $request->getActionName() !== "add" && $quote->getIsMultiShipping();
135+
}
136+
}

0 commit comments

Comments
 (0)