Skip to content

Commit 7e7befb

Browse files
committed
Merge remote-tracking branch 'magento-mpi/MC-17518' into MPI-PR
2 parents c12029b + 1f6deb8 commit 7e7befb

File tree

10 files changed

+535
-112
lines changed

10 files changed

+535
-112
lines changed

app/code/Magento/Multishipping/Controller/Checkout/Plugin.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Plugin;
9+
10+
use Magento\Checkout\Model\Cart;
11+
use Magento\Framework\App\Action\Action;
12+
13+
/**
14+
* Turns Off Multishipping mode for Quote.
15+
*/
16+
class DisableMultishippingMode
17+
{
18+
/**
19+
* @var Cart
20+
*/
21+
private $cart;
22+
23+
/**
24+
* @param Cart $cart
25+
*/
26+
public function __construct(
27+
Cart $cart
28+
) {
29+
$this->cart = $cart;
30+
}
31+
32+
/**
33+
* Disable multishipping
34+
*
35+
* @param Action $subject
36+
* @return void
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function beforeExecute(Action $subject)
40+
{
41+
$quote = $this->cart->getQuote();
42+
if ($quote->getIsMultiShipping()) {
43+
$quote->setIsMultiShipping(0);
44+
$extensionAttributes = $quote->getExtensionAttributes();
45+
if ($extensionAttributes && $extensionAttributes->getShippingAssignments()) {
46+
$extensionAttributes->setShippingAssignments([]);
47+
}
48+
$this->cart->saveQuote();
49+
}
50+
}
51+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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\Plugin;
9+
10+
use Magento\Framework\Api\SearchResultsInterface;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\Quote\Model\Quote;
14+
use Magento\Quote\Model\Quote\ShippingAssignment\ShippingProcessor;
15+
use Magento\Quote\Model\ShippingAssignmentFactory;
16+
17+
/**
18+
* Plugin for multishipping quote processing.
19+
*/
20+
class MultishippingQuoteRepository
21+
{
22+
/**
23+
* @var ShippingAssignmentFactory
24+
*/
25+
private $shippingAssignmentFactory;
26+
27+
/**
28+
* @var ShippingProcessor
29+
*/
30+
private $shippingProcessor;
31+
32+
/**
33+
* @param ShippingAssignmentFactory $shippingAssignmentFactory
34+
* @param ShippingProcessor $shippingProcessor
35+
*/
36+
public function __construct(
37+
ShippingAssignmentFactory $shippingAssignmentFactory,
38+
ShippingProcessor $shippingProcessor
39+
) {
40+
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
41+
$this->shippingProcessor = $shippingProcessor;
42+
}
43+
44+
/**
45+
* Process multishipping quote for get.
46+
*
47+
* @param CartRepositoryInterface $subject
48+
* @param CartInterface $result
49+
* @return CartInterface
50+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
51+
*/
52+
public function afterGet(
53+
CartRepositoryInterface $subject,
54+
CartInterface $result
55+
) {
56+
return $this->processQuote($result);
57+
}
58+
59+
/**
60+
* Process multishipping quote for get list.
61+
*
62+
* @param CartRepositoryInterface $subject
63+
* @param SearchResultsInterface $result
64+
*
65+
* @return SearchResultsInterface
66+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
67+
*/
68+
public function afterGetList(
69+
CartRepositoryInterface $subject,
70+
SearchResultsInterface $result
71+
) {
72+
$items = [];
73+
foreach ($result->getItems() as $item) {
74+
$items[] = $this->processQuote($item);
75+
}
76+
$result->setItems($items);
77+
78+
return $result;
79+
}
80+
81+
/**
82+
* Remove shipping assignments for multishipping quote.
83+
*
84+
* @param CartRepositoryInterface $subject
85+
* @param CartInterface $quote
86+
* @return array
87+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
88+
*/
89+
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote)
90+
{
91+
$extensionAttributes = $quote->getExtensionAttributes();
92+
if ($quote->getIsMultiShipping() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
93+
$quote->getExtensionAttributes()->setShippingAssignments([]);
94+
}
95+
96+
return [$quote];
97+
}
98+
99+
/**
100+
* Set shipping assignments for multishipping quote according to customer selection.
101+
*
102+
* @param CartInterface $quote
103+
* @return CartInterface
104+
*/
105+
private function processQuote(CartInterface $quote): CartInterface
106+
{
107+
if (!$quote->getIsMultiShipping() || !$quote instanceof Quote) {
108+
return $quote;
109+
}
110+
111+
if ($quote->getExtensionAttributes() && $quote->getExtensionAttributes()->getShippingAssignments()) {
112+
$shippingAssignments = [];
113+
$addresses = $quote->getAllAddresses();
114+
115+
foreach ($addresses as $address) {
116+
$quoteItems = $this->getQuoteItems($quote, $address);
117+
if (!empty($quoteItems)) {
118+
$shippingAssignment = $this->shippingAssignmentFactory->create();
119+
$shippingAssignment->setItems($quoteItems);
120+
$shippingAssignment->setShipping($this->shippingProcessor->create($address));
121+
$shippingAssignments[] = $shippingAssignment;
122+
}
123+
}
124+
125+
if (!empty($shippingAssignments)) {
126+
$quote->getExtensionAttributes()->setShippingAssignments($shippingAssignments);
127+
}
128+
}
129+
130+
return $quote;
131+
}
132+
133+
/**
134+
* Returns quote items assigned to address.
135+
*
136+
* @param Quote $quote
137+
* @param Quote\Address $address
138+
* @return Quote\Item[]
139+
*/
140+
private function getQuoteItems(Quote $quote, Quote\Address $address): array
141+
{
142+
$quoteItems = [];
143+
foreach ($address->getItemsCollection() as $addressItem) {
144+
$quoteItem = $quote->getItemById($addressItem->getQuoteItemId());
145+
if ($quoteItem) {
146+
$multishippingQuoteItem = clone $quoteItem;
147+
$qty = $addressItem->getQty();
148+
$sku = $multishippingQuoteItem->getSku();
149+
if (isset($quoteItems[$sku])) {
150+
$qty += $quoteItems[$sku]->getQty();
151+
}
152+
$multishippingQuoteItem->setQty($qty);
153+
$quoteItems[$sku] = $multishippingQuoteItem;
154+
}
155+
}
156+
157+
return array_values($quoteItems);
158+
}
159+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Plugin;
9+
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor;
12+
13+
/**
14+
* Resets quote shipping assignments when item is removed from multishipping quote.
15+
*/
16+
class ResetShippingAssigment
17+
{
18+
/**
19+
* @var ShippingAssignmentProcessor
20+
*/
21+
private $shippingAssignmentProcessor;
22+
23+
/**
24+
* @param ShippingAssignmentProcessor $shippingAssignmentProcessor
25+
*/
26+
public function __construct(
27+
ShippingAssignmentProcessor $shippingAssignmentProcessor
28+
) {
29+
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor;
30+
}
31+
32+
/**
33+
* Resets quote shipping assignments when item is removed from multishipping quote.
34+
*
35+
* @param Quote $subject
36+
* @param mixed $itemId
37+
*
38+
* @return array
39+
*/
40+
public function beforeRemoveItem(Quote $subject, $itemId): array
41+
{
42+
if ($subject->getIsMultiShipping()) {
43+
$extensionAttributes = $subject->getExtensionAttributes();
44+
if ($extensionAttributes && $extensionAttributes->getShippingAssignments()) {
45+
$shippingAssignment = $this->shippingAssignmentProcessor->create($subject);
46+
$extensionAttributes->setShippingAssignments([$shippingAssignment]);
47+
}
48+
}
49+
50+
return [$itemId];
51+
}
52+
}

app/code/Magento/Multishipping/Test/Unit/Controller/Checkout/PluginTest.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)