|
| 1 | +diff --git a/vendor/magento/module-checkout/Model/AddressComparator.php b/vendor/magento/module-checkout/Model/AddressComparator.php |
| 2 | +index c285f15e4e40..f47bab2ba6a4 100644 |
| 3 | +--- a/vendor/magento/module-checkout/Model/AddressComparator.php |
| 4 | ++++ b/vendor/magento/module-checkout/Model/AddressComparator.php |
| 5 | +@@ -43,7 +43,7 @@ public function isEqual(?AddressInterface $address1, ?AddressInterface $address2 |
| 6 | + (int)$address2->getCustomerAddressId()); |
| 7 | + } else { |
| 8 | + $addressKeys = array_intersect_key($address1->getData(), $address2->getData()); |
| 9 | +- $removeKeys = ['address_type', 'region_code', 'save_in_address_book']; |
| 10 | ++ $removeKeys = ['address_type', 'region_code', 'save_in_address_book', 'customer_address_id']; |
| 11 | + $addressKeys = array_diff_key($addressKeys, array_flip($removeKeys)); |
| 12 | + |
| 13 | + $address1Data = array_intersect_key($address1->getData(), $addressKeys); |
| 14 | +diff --git a/vendor/magento/module-checkout/Model/PaymentInformationManagement.php b/vendor/magento/module-checkout/Model/PaymentInformationManagement.php |
| 15 | +index 41867d3c8350..2913622e4abd 100644 |
| 16 | +--- a/vendor/magento/module-checkout/Model/PaymentInformationManagement.php |
| 17 | ++++ b/vendor/magento/module-checkout/Model/PaymentInformationManagement.php |
| 18 | +@@ -10,11 +10,15 @@ |
| 19 | + use Magento\Checkout\Api\PaymentProcessingRateLimiterInterface; |
| 20 | + use Magento\Checkout\Api\PaymentSavingRateLimiterInterface; |
| 21 | + use Magento\Customer\Api\AddressRepositoryInterface; |
| 22 | ++use Magento\Customer\Api\Data\AddressInterface; |
| 23 | + use Magento\Framework\App\ObjectManager; |
| 24 | + use Magento\Framework\Exception\CouldNotSaveException; |
| 25 | + use Magento\Framework\Exception\LocalizedException; |
| 26 | + use Magento\Quote\Api\CartRepositoryInterface; |
| 27 | ++use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface; |
| 28 | ++use Magento\Quote\Api\Data\PaymentInterface; |
| 29 | + use Magento\Quote\Model\Quote; |
| 30 | ++use Magento\Quote\Model\Quote\Address; |
| 31 | + use Psr\Log\LoggerInterface; |
| 32 | + |
| 33 | + /** |
| 34 | +@@ -137,8 +141,8 @@ public function __construct( |
| 35 | + */ |
| 36 | + public function savePaymentInformationAndPlaceOrder( |
| 37 | + $cartId, |
| 38 | +- \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, |
| 39 | +- \Magento\Quote\Api\Data\AddressInterface $billingAddress = null |
| 40 | ++ PaymentInterface $paymentMethod, |
| 41 | ++ QuoteAddressInterface $billingAddress = null |
| 42 | + ) { |
| 43 | + $this->paymentRateLimiter->limit(); |
| 44 | + try { |
| 45 | +@@ -175,8 +179,8 @@ public function savePaymentInformationAndPlaceOrder( |
| 46 | + */ |
| 47 | + public function savePaymentInformation( |
| 48 | + $cartId, |
| 49 | +- \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, |
| 50 | +- \Magento\Quote\Api\Data\AddressInterface $billingAddress = null |
| 51 | ++ PaymentInterface $paymentMethod, |
| 52 | ++ QuoteAddressInterface $billingAddress = null |
| 53 | + ) { |
| 54 | + if (!$this->saveRateLimiterDisabled) { |
| 55 | + try { |
| 56 | +@@ -196,6 +200,7 @@ public function savePaymentInformation( |
| 57 | + //It's necessary to verify the price rules with the customer data |
| 58 | + $billingAddress->setCustomerId($customerId); |
| 59 | + } |
| 60 | ++ $this->updateCustomerBillingAddressId($quote, $billingAddress); |
| 61 | + $quote->removeAddress($quote->getBillingAddress()->getId()); |
| 62 | + $quote->setBillingAddress($billingAddress); |
| 63 | + $quote->setDataChanges(true); |
| 64 | +@@ -240,18 +245,12 @@ private function processShippingAddress(Quote $quote): void |
| 65 | + $shippingAddress->setSameAsBilling(1); |
| 66 | + } |
| 67 | + // Save new address in the customer address book and set it id for billing and shipping quote addresses. |
| 68 | +- if ($shippingAddress->getSameAsBilling() && $shippingAddress->getSaveInAddressBook()) { |
| 69 | ++ if ($shippingAddress->getSameAsBilling() && |
| 70 | ++ $shippingAddress->getSaveInAddressBook() && |
| 71 | ++ !$shippingAddress->getCustomerAddressId() |
| 72 | ++ ) { |
| 73 | + $shippingAddressData = $shippingAddress->exportCustomerAddress(); |
| 74 | +- $customer = $quote->getCustomer(); |
| 75 | +- $hasDefaultBilling = (bool)$customer->getDefaultBilling(); |
| 76 | +- $hasDefaultShipping = (bool)$customer->getDefaultShipping(); |
| 77 | +- if (!$hasDefaultShipping) { |
| 78 | +- //Make provided address as default shipping address |
| 79 | +- $shippingAddressData->setIsDefaultShipping(true); |
| 80 | +- if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) { |
| 81 | +- $shippingAddressData->setIsDefaultBilling(true); |
| 82 | +- } |
| 83 | +- } |
| 84 | ++ $this->saveAddressesAsDefault($quote, $shippingAddressData, $billingAddress); |
| 85 | + $shippingAddressData->setCustomerId($quote->getCustomerId()); |
| 86 | + $this->addressRepository->save($shippingAddressData); |
| 87 | + $quote->addCustomerAddress($shippingAddressData); |
| 88 | +@@ -260,4 +259,47 @@ private function processShippingAddress(Quote $quote): void |
| 89 | + $billingAddress->setCustomerAddressId($shippingAddressData->getId()); |
| 90 | + } |
| 91 | + } |
| 92 | ++ |
| 93 | ++ /** |
| 94 | ++ * Update customer billing address ID if the address is the same as the quote billing address. |
| 95 | ++ * |
| 96 | ++ * @param Quote $quote |
| 97 | ++ * @param QuoteAddressInterface $billingAddress |
| 98 | ++ * @return void |
| 99 | ++ */ |
| 100 | ++ private function updateCustomerBillingAddressId(Quote $quote, QuoteAddressInterface $billingAddress): void |
| 101 | ++ { |
| 102 | ++ $quoteBillingAddress = $quote->getBillingAddress(); |
| 103 | ++ if (!$billingAddress->getCustomerAddressId() && |
| 104 | ++ $quoteBillingAddress->getCustomerAddressId() && |
| 105 | ++ $this->addressComparator->isEqual($billingAddress, $quoteBillingAddress) |
| 106 | ++ ) { |
| 107 | ++ $billingAddress->setCustomerAddressId($quoteBillingAddress->getCustomerAddressId()); |
| 108 | ++ } |
| 109 | ++ } |
| 110 | ++ |
| 111 | ++ /** |
| 112 | ++ * Save addresses as default shipping/ billing if they are not set yet. |
| 113 | ++ * |
| 114 | ++ * @param Quote $quote |
| 115 | ++ * @param AddressInterface $shippingAddressData |
| 116 | ++ * @param Address $billingAddress |
| 117 | ++ * @return void |
| 118 | ++ */ |
| 119 | ++ private function saveAddressesAsDefault( |
| 120 | ++ Quote $quote, |
| 121 | ++ AddressInterface $shippingAddressData, |
| 122 | ++ Address $billingAddress |
| 123 | ++ ): void { |
| 124 | ++ $customer = $quote->getCustomer(); |
| 125 | ++ $hasDefaultBilling = (bool)$customer->getDefaultBilling(); |
| 126 | ++ $hasDefaultShipping = (bool)$customer->getDefaultShipping(); |
| 127 | ++ if (!$hasDefaultShipping) { |
| 128 | ++ //Make provided address as default shipping address |
| 129 | ++ $shippingAddressData->setIsDefaultShipping(true); |
| 130 | ++ if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) { |
| 131 | ++ $shippingAddressData->setIsDefaultBilling(true); |
| 132 | ++ } |
| 133 | ++ } |
| 134 | ++ } |
| 135 | + } |
| 136 | +diff --git a/vendor/magento/module-checkout/Model/ShippingInformationManagement.php b/vendor/magento/module-checkout/Model/ShippingInformationManagement.php |
| 137 | +index f08c48c55efa..0bb025508aee 100644 |
| 138 | +--- a/vendor/magento/module-checkout/Model/ShippingInformationManagement.php |
| 139 | ++++ b/vendor/magento/module-checkout/Model/ShippingInformationManagement.php |
| 140 | +@@ -102,6 +102,11 @@ class ShippingInformationManagement implements ShippingInformationManagementInte |
| 141 | + */ |
| 142 | + private $shippingFactory; |
| 143 | + |
| 144 | ++ /** |
| 145 | ++ * @var AddressComparatorInterface |
| 146 | ++ */ |
| 147 | ++ private $addressComparator; |
| 148 | ++ |
| 149 | + /** |
| 150 | + * @param PaymentMethodManagementInterface $paymentMethodManagement |
| 151 | + * @param PaymentDetailsFactory $paymentDetailsFactory |
| 152 | +@@ -115,6 +120,7 @@ class ShippingInformationManagement implements ShippingInformationManagementInte |
| 153 | + * @param CartExtensionFactory|null $cartExtensionFactory |
| 154 | + * @param ShippingAssignmentFactory|null $shippingAssignmentFactory |
| 155 | + * @param ShippingFactory|null $shippingFactory |
| 156 | ++ * @param AddressComparatorInterface|null $addressComparator |
| 157 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 158 | + */ |
| 159 | + public function __construct( |
| 160 | +@@ -129,7 +135,8 @@ public function __construct( |
| 161 | + TotalsCollector $totalsCollector, |
| 162 | + CartExtensionFactory $cartExtensionFactory = null, |
| 163 | + ShippingAssignmentFactory $shippingAssignmentFactory = null, |
| 164 | +- ShippingFactory $shippingFactory = null |
| 165 | ++ ShippingFactory $shippingFactory = null, |
| 166 | ++ ?AddressComparatorInterface $addressComparator = null, |
| 167 | + ) { |
| 168 | + $this->paymentMethodManagement = $paymentMethodManagement; |
| 169 | + $this->paymentDetailsFactory = $paymentDetailsFactory; |
| 170 | +@@ -146,6 +153,8 @@ public function __construct( |
| 171 | + ->get(ShippingAssignmentFactory::class); |
| 172 | + $this->shippingFactory = $shippingFactory ?: ObjectManager::getInstance() |
| 173 | + ->get(ShippingFactory::class); |
| 174 | ++ $this->addressComparator = $addressComparator |
| 175 | ++ ?? ObjectManager::getInstance()->get(AddressComparatorInterface::class); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | +@@ -168,7 +177,7 @@ public function saveAddressInformation( |
| 180 | + |
| 181 | + $address = $addressInformation->getShippingAddress(); |
| 182 | + $this->validateAddress($address); |
| 183 | +- |
| 184 | ++ $this->updateCustomerShippingAddressId($quote, $address); |
| 185 | + if (!$address->getCustomerAddressId()) { |
| 186 | + $address->setCustomerAddressId(null); |
| 187 | + } |
| 188 | +@@ -176,6 +185,7 @@ public function saveAddressInformation( |
| 189 | + try { |
| 190 | + $billingAddress = $addressInformation->getBillingAddress(); |
| 191 | + if ($billingAddress) { |
| 192 | ++ $this->updateCustomerBillingAddressId($quote, $billingAddress); |
| 193 | + if (!$billingAddress->getCustomerAddressId()) { |
| 194 | + $billingAddress->setCustomerAddressId(null); |
| 195 | + } |
| 196 | +@@ -293,4 +303,39 @@ private function prepareShippingAssignment( |
| 197 | + $cartExtension->setShippingAssignments([$shippingAssignment]); |
| 198 | + return $quote->setExtensionAttributes($cartExtension); |
| 199 | + } |
| 200 | ++ |
| 201 | ++ /** |
| 202 | ++ * Update customer shipping address ID if the address is the same as the quote shipping address. |
| 203 | ++ * |
| 204 | ++ * @param Quote $quote |
| 205 | ++ * @param AddressInterface $address |
| 206 | ++ * @return void |
| 207 | ++ */ |
| 208 | ++ private function updateCustomerShippingAddressId(Quote $quote, AddressInterface $address): void |
| 209 | ++ { |
| 210 | ++ $quoteShippingAddress = $quote->getShippingAddress(); |
| 211 | ++ if (!$address->getCustomerAddressId() && |
| 212 | ++ $quoteShippingAddress->getCustomerAddressId() && |
| 213 | ++ $this->addressComparator->isEqual($address, $quoteShippingAddress) |
| 214 | ++ ) { |
| 215 | ++ $address->setCustomerAddressId($quoteShippingAddress->getCustomerAddressId()); |
| 216 | ++ } |
| 217 | ++ } |
| 218 | ++ |
| 219 | ++ /** |
| 220 | ++ * Update customer billing address ID if the address is the same as the quote billing address. |
| 221 | ++ * |
| 222 | ++ * @param Quote $quote |
| 223 | ++ * @param AddressInterface $billingAddress |
| 224 | ++ * @return void |
| 225 | ++ */ |
| 226 | ++ private function updateCustomerBillingAddressId(Quote $quote, AddressInterface $billingAddress): void |
| 227 | ++ { |
| 228 | ++ $quoteBillingAddress = $quote->getBillingAddress(); |
| 229 | ++ if ($quoteBillingAddress->getCustomerAddressId() && |
| 230 | ++ $this->addressComparator->isEqual($billingAddress, $quoteBillingAddress) |
| 231 | ++ ) { |
| 232 | ++ $billingAddress->setCustomerAddressId($quoteBillingAddress->getCustomerAddressId()); |
| 233 | ++ } |
| 234 | ++ } |
| 235 | + } |
| 236 | + |
0 commit comments