Skip to content

Commit 629423a

Browse files
committed
ACP2E-1826: [Cloud] reorder shippinng zipcode does not update the latest shipping zip code
1 parent 59f2672 commit 629423a

File tree

5 files changed

+295
-59
lines changed

5 files changed

+295
-59
lines changed

app/code/Magento/Quote/Model/Quote/Address/BillingAddressPersister.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66
namespace Magento\Quote\Model\Quote\Address;
77

8+
use Magento\Customer\Api\AddressRepositoryInterface;
89
use Magento\Framework\Exception\InputException;
9-
use Magento\Quote\Api\Data\CartInterface;
10-
use Magento\Quote\Api\Data\AddressInterface;
10+
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
1214
use Magento\Quote\Model\QuoteAddressValidator;
13-
use Magento\Customer\Api\AddressRepositoryInterface;
1415

1516
/**
1617
* Saves billing address for quotes.
@@ -47,30 +48,28 @@ public function __construct(
4748
* @param bool $useForShipping
4849
* @return void
4950
* @throws NoSuchEntityException
50-
* @throws InputException
51+
* @throws InputException|LocalizedException
5152
*/
5253
public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false)
5354
{
5455
/** @var \Magento\Quote\Model\Quote $quote */
5556
$this->addressValidator->validateForCart($quote, $address);
5657
$customerAddressId = $address->getCustomerAddressId();
5758
$shippingAddress = null;
58-
$addressData = [];
59-
6059
if ($useForShipping) {
6160
$shippingAddress = $address;
6261
}
6362
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
6463
if ($customerAddressId) {
6564
try {
6665
$addressData = $this->addressRepository->getById($customerAddressId);
66+
$address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
67+
if ($useForShipping) {
68+
$shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
69+
$shippingAddress->setSaveInAddressBook($saveInAddressBook);
70+
}
6771
} catch (NoSuchEntityException $e) {
68-
// do nothing if customer is not found by id
69-
}
70-
$address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
71-
if ($useForShipping) {
72-
$shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
73-
$shippingAddress->setSaveInAddressBook($saveInAddressBook);
72+
$address->setCustomerAddressId(null);
7473
}
7574
} elseif ($quote->getCustomerId()) {
7675
$address->setEmail($quote->getCustomerEmail());
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Quote\Model\Quote\Address;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Quote\Api\Data\AddressInterface;
15+
use Magento\Quote\Api\Data\CartInterface;
16+
use Magento\Quote\Model\QuoteAddressValidator;
17+
18+
class ShippingAddressPersister
19+
{
20+
/**
21+
* @var QuoteAddressValidator
22+
*/
23+
private $addressValidator;
24+
25+
/**
26+
* @var AddressRepositoryInterface
27+
*/
28+
private $addressRepository;
29+
30+
/**
31+
* @param QuoteAddressValidator $addressValidator
32+
* @param AddressRepositoryInterface $addressRepository
33+
*/
34+
public function __construct(
35+
QuoteAddressValidator $addressValidator,
36+
AddressRepositoryInterface $addressRepository
37+
) {
38+
$this->addressValidator = $addressValidator;
39+
$this->addressRepository = $addressRepository;
40+
}
41+
42+
/**
43+
* Save address for shipping.
44+
*
45+
* @param CartInterface $quote
46+
* @param AddressInterface $address
47+
* @return void
48+
* @throws InputException
49+
* @throws LocalizedException
50+
* @throws NoSuchEntityException
51+
*/
52+
public function save(CartInterface $quote, AddressInterface $address): void
53+
{
54+
$this->addressValidator->validateForCart($quote, $address);
55+
$customerAddressId = $address->getCustomerAddressId();
56+
57+
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
58+
if ($customerAddressId) {
59+
try {
60+
$addressData = $this->addressRepository->getById($customerAddressId);
61+
$address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
62+
} catch (NoSuchEntityException $e) {
63+
$address->setCustomerAddressId(null);
64+
}
65+
} elseif ($quote->getCustomerId()) {
66+
$address->setEmail($quote->getCustomerEmail());
67+
}
68+
$address->setSaveInAddressBook($saveInAddressBook);
69+
$quote->setShippingAddress($address);
70+
}
71+
}

app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,47 @@
77

88
namespace Magento\Quote\Model\QuoteRepository;
99

10-
use Magento\Quote\Api\Data\CartInterface;
10+
use Magento\Backend\Model\Session\Quote as QuoteSession;
1111
use Magento\Customer\Api\AddressRepositoryInterface;
1212
use Magento\Framework\App\ObjectManager;
13-
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Exception\CouldNotSaveException;
1414
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Quote\Api\Data\AddressInterface;
1518
use Magento\Quote\Api\Data\AddressInterfaceFactory;
19+
use Magento\Quote\Api\Data\CartInterface;
20+
use Magento\Quote\Model\Quote\Address\BillingAddressPersister;
21+
use Magento\Quote\Model\Quote\Address\ShippingAddressPersister;
22+
use Magento\Quote\Model\Quote\Item\CartItemPersister;
23+
use Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister;
24+
use Magento\Quote\Model\ResourceModel\Quote;
1625

1726
/**
1827
* Handler for saving quote.
28+
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1931
*/
2032
class SaveHandler
2133
{
2234
/**
23-
* @var \Magento\Quote\Model\Quote\Item\CartItemPersister
35+
* @var CartItemPersister
2436
*/
2537
private $cartItemPersister;
2638

2739
/**
28-
* @var \Magento\Quote\Model\Quote\Address\BillingAddressPersister
40+
* @var BillingAddressPersister
2941
*/
3042
private $billingAddressPersister;
3143

3244
/**
33-
* @var \Magento\Quote\Model\ResourceModel\Quote
45+
* @var Quote
3446
*/
3547
private $quoteResourceModel;
3648

3749
/**
38-
* @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister
50+
* @var ShippingAssignmentPersister
3951
*/
4052
private $shippingAssignmentPersister;
4153

@@ -50,29 +62,46 @@ class SaveHandler
5062
private $quoteAddressFactory;
5163

5264
/**
53-
* @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
54-
* @param \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister
55-
* @param \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister
56-
* @param \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
57-
* @param AddressRepositoryInterface $addressRepository
65+
* @var ShippingAddressPersister
66+
*/
67+
private $shippingAddressPersister;
68+
69+
/**
70+
* @var QuoteSession
71+
*/
72+
private $quoteSession;
73+
74+
/**
75+
* @param Quote $quoteResource
76+
* @param CartItemPersister $cartItemPersister
77+
* @param BillingAddressPersister $billingAddressPersister
78+
* @param ShippingAssignmentPersister $shippingAssignmentPersister
79+
* @param AddressRepositoryInterface|null $addressRepository
5880
* @param AddressInterfaceFactory|null $addressFactory
81+
* @param ShippingAddressPersister|null $shippingAddressPersister
82+
* @param QuoteSession|null $quoteSession
5983
*/
6084
public function __construct(
61-
\Magento\Quote\Model\ResourceModel\Quote $quoteResource,
62-
\Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister,
63-
\Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister,
64-
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister,
85+
Quote $quoteResource,
86+
CartItemPersister $cartItemPersister,
87+
BillingAddressPersister $billingAddressPersister,
88+
ShippingAssignmentPersister $shippingAssignmentPersister,
6589
AddressRepositoryInterface $addressRepository = null,
66-
AddressInterfaceFactory $addressFactory = null
90+
AddressInterfaceFactory $addressFactory = null,
91+
ShippingAddressPersister $shippingAddressPersister = null,
92+
QuoteSession $quoteSession = null
6793
) {
6894
$this->quoteResourceModel = $quoteResource;
6995
$this->cartItemPersister = $cartItemPersister;
7096
$this->billingAddressPersister = $billingAddressPersister;
7197
$this->shippingAssignmentPersister = $shippingAssignmentPersister;
7298
$this->addressRepository = $addressRepository
7399
?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
74-
$this->quoteAddressFactory = $addressFactory ?:ObjectManager::getInstance()
100+
$this->quoteAddressFactory = $addressFactory ?: ObjectManager::getInstance()
75101
->get(AddressInterfaceFactory::class);
102+
$this->shippingAddressPersister = $shippingAddressPersister
103+
?: ObjectManager::getInstance()->get(ShippingAddressPersister::class);
104+
$this->quoteSession = $quoteSession ?: ObjectManager::getInstance()->get(QuoteSession::class);
76105
}
77106

78107
/**
@@ -81,18 +110,16 @@ public function __construct(
81110
* @param CartInterface $quote
82111
* @return CartInterface
83112
* @throws InputException
84-
* @throws \Magento\Framework\Exception\CouldNotSaveException
85-
* @throws \Magento\Framework\Exception\LocalizedException
113+
* @throws CouldNotSaveException
114+
* @throws LocalizedException
86115
*/
87116
public function save(CartInterface $quote)
88117
{
89-
/** @var \Magento\Quote\Model\Quote $quote */
90118
// Quote Item processing
91119
$items = $quote->getItems();
92120

93121
if ($items) {
94122
foreach ($items as $item) {
95-
/** @var \Magento\Quote\Model\Quote\Item $item */
96123
if (!$item->isDeleted()) {
97124
$quote->setLastAddedItem($this->cartItemPersister->save($quote, $item));
98125
} elseif (count($items) === 1) {
@@ -104,33 +131,50 @@ public function save(CartInterface $quote)
104131

105132
// Billing Address processing
106133
$billingAddress = $quote->getBillingAddress();
107-
108134
if ($billingAddress) {
109-
if ($billingAddress->getCustomerAddressId()) {
110-
try {
111-
$this->addressRepository->getById($billingAddress->getCustomerAddressId());
112-
} catch (NoSuchEntityException $e) {
113-
$billingAddress->setCustomerAddressId(null);
114-
}
115-
}
116-
135+
$this->processAddress($billingAddress);
117136
$this->billingAddressPersister->save($quote, $billingAddress);
118137
}
119138

139+
// Shipping Address processing
140+
if ($this->quoteSession->getData(('reordered'))) {
141+
$shippingAddress = $this->processAddress($quote->getShippingAddress());
142+
$this->shippingAddressPersister->save($quote, $shippingAddress);
143+
}
144+
120145
$this->processShippingAssignment($quote);
121146
$this->quoteResourceModel->save($quote->collectTotals());
122147

123148
return $quote;
124149
}
125150

151+
/**
152+
* Process address for customer address Id
153+
*
154+
* @param AddressInterface $address
155+
* @return AddressInterface
156+
* @throws LocalizedException
157+
*/
158+
private function processAddress(AddressInterface $address): AddressInterface
159+
{
160+
if ($address->getCustomerAddressId()) {
161+
try {
162+
$this->addressRepository->getById($address->getCustomerAddressId());
163+
} catch (NoSuchEntityException $e) {
164+
$address->setCustomerAddressId(null);
165+
}
166+
}
167+
return $address;
168+
}
169+
126170
/**
127171
* Process shipping assignment
128172
*
129-
* @param \Magento\Quote\Model\Quote $quote
173+
* @param CartInterface $quote
130174
* @return void
131175
* @throws InputException
132176
*/
133-
private function processShippingAssignment($quote)
177+
private function processShippingAssignment(CartInterface $quote)
134178
{
135179
// Shipping Assignments processing
136180
$extensionAttributes = $quote->getExtensionAttributes();

app/code/Magento/Sales/view/adminhtml/web/order/create/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
}
192192
this.selectAddressEvent = false;
193193

194-
var data = this.serializeData(container);
194+
let data = this.serializeData(container).toObject();
195195
data[el.name] = id;
196196

197197
this.resetPaymentMethod();

0 commit comments

Comments
 (0)