diff --git a/vendor/magento/module-checkout/Model/AddressComparator.php b/vendor/magento/module-checkout/Model/AddressComparator.php index c285f15e4e4..f47bab2ba6a 100644 --- a/vendor/magento/module-checkout/Model/AddressComparator.php +++ b/vendor/magento/module-checkout/Model/AddressComparator.php @@ -43,7 +43,7 @@ class AddressComparator implements AddressComparatorInterface (int)$address2->getCustomerAddressId()); } else { $addressKeys = array_intersect_key($address1->getData(), $address2->getData()); - $removeKeys = ['address_type', 'region_code', 'save_in_address_book']; + $removeKeys = ['address_type', 'region_code', 'save_in_address_book', 'customer_address_id']; $addressKeys = array_diff_key($addressKeys, array_flip($removeKeys)); $address1Data = array_intersect_key($address1->getData(), $addressKeys); diff --git a/vendor/magento/module-checkout/Model/PaymentInformationManagement.php b/vendor/magento/module-checkout/Model/PaymentInformationManagement.php index 41867d3c835..2913622e4ab 100644 --- a/vendor/magento/module-checkout/Model/PaymentInformationManagement.php +++ b/vendor/magento/module-checkout/Model/PaymentInformationManagement.php @@ -10,11 +10,15 @@ use Magento\Checkout\Api\Exception\PaymentProcessingRateLimitExceededException; use Magento\Checkout\Api\PaymentProcessingRateLimiterInterface; use Magento\Checkout\Api\PaymentSavingRateLimiterInterface; use Magento\Customer\Api\AddressRepositoryInterface; +use Magento\Customer\Api\Data\AddressInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Api\CartRepositoryInterface; +use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface; +use Magento\Quote\Api\Data\PaymentInterface; use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Address; use Psr\Log\LoggerInterface; /** @@ -137,8 +141,8 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor */ public function savePaymentInformationAndPlaceOrder( $cartId, - \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, - \Magento\Quote\Api\Data\AddressInterface $billingAddress = null + PaymentInterface $paymentMethod, + QuoteAddressInterface $billingAddress = null ) { $this->paymentRateLimiter->limit(); try { @@ -175,8 +179,8 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor */ public function savePaymentInformation( $cartId, - \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, - \Magento\Quote\Api\Data\AddressInterface $billingAddress = null + PaymentInterface $paymentMethod, + QuoteAddressInterface $billingAddress = null ) { if (!$this->saveRateLimiterDisabled) { try { @@ -196,6 +200,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor //It's necessary to verify the price rules with the customer data $billingAddress->setCustomerId($customerId); } + $this->updateCustomerBillingAddressId($quote, $billingAddress); $quote->removeAddress($quote->getBillingAddress()->getId()); $quote->setBillingAddress($billingAddress); $quote->setDataChanges(true); @@ -240,18 +245,12 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor $shippingAddress->setSameAsBilling(1); } // Save new address in the customer address book and set it id for billing and shipping quote addresses. - if ($shippingAddress->getSameAsBilling() && $shippingAddress->getSaveInAddressBook()) { + if ($shippingAddress->getSameAsBilling() && + $shippingAddress->getSaveInAddressBook() && + !$shippingAddress->getCustomerAddressId() + ) { $shippingAddressData = $shippingAddress->exportCustomerAddress(); - $customer = $quote->getCustomer(); - $hasDefaultBilling = (bool)$customer->getDefaultBilling(); - $hasDefaultShipping = (bool)$customer->getDefaultShipping(); - if (!$hasDefaultShipping) { - //Make provided address as default shipping address - $shippingAddressData->setIsDefaultShipping(true); - if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) { - $shippingAddressData->setIsDefaultBilling(true); - } - } + $this->saveAddressesAsDefault($quote, $shippingAddressData, $billingAddress); $shippingAddressData->setCustomerId($quote->getCustomerId()); $this->addressRepository->save($shippingAddressData); $quote->addCustomerAddress($shippingAddressData); @@ -260,4 +259,47 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor $billingAddress->setCustomerAddressId($shippingAddressData->getId()); } } + + /** + * Update customer billing address ID if the address is the same as the quote billing address. + * + * @param Quote $quote + * @param QuoteAddressInterface $billingAddress + * @return void + */ + private function updateCustomerBillingAddressId(Quote $quote, QuoteAddressInterface $billingAddress): void + { + $quoteBillingAddress = $quote->getBillingAddress(); + if (!$billingAddress->getCustomerAddressId() && + $quoteBillingAddress->getCustomerAddressId() && + $this->addressComparator->isEqual($billingAddress, $quoteBillingAddress) + ) { + $billingAddress->setCustomerAddressId($quoteBillingAddress->getCustomerAddressId()); + } + } + + /** + * Save addresses as default shipping/ billing if they are not set yet. + * + * @param Quote $quote + * @param AddressInterface $shippingAddressData + * @param Address $billingAddress + * @return void + */ + private function saveAddressesAsDefault( + Quote $quote, + AddressInterface $shippingAddressData, + Address $billingAddress + ): void { + $customer = $quote->getCustomer(); + $hasDefaultBilling = (bool)$customer->getDefaultBilling(); + $hasDefaultShipping = (bool)$customer->getDefaultShipping(); + if (!$hasDefaultShipping) { + //Make provided address as default shipping address + $shippingAddressData->setIsDefaultShipping(true); + if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) { + $shippingAddressData->setIsDefaultBilling(true); + } + } + } } diff --git a/vendor/magento/module-checkout/Model/ShippingInformationManagement.php b/vendor/magento/module-checkout/Model/ShippingInformationManagement.php index f08c48c55ef..0bb025508ae 100644 --- a/vendor/magento/module-checkout/Model/ShippingInformationManagement.php +++ b/vendor/magento/module-checkout/Model/ShippingInformationManagement.php @@ -102,6 +102,11 @@ class ShippingInformationManagement implements ShippingInformationManagementInte */ private $shippingFactory; + /** + * @var AddressComparatorInterface + */ + private $addressComparator; + /** * @param PaymentMethodManagementInterface $paymentMethodManagement * @param PaymentDetailsFactory $paymentDetailsFactory @@ -115,6 +120,7 @@ class ShippingInformationManagement implements ShippingInformationManagementInte * @param CartExtensionFactory|null $cartExtensionFactory * @param ShippingAssignmentFactory|null $shippingAssignmentFactory * @param ShippingFactory|null $shippingFactory + * @param AddressComparatorInterface|null $addressComparator * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -129,7 +135,8 @@ class ShippingInformationManagement implements ShippingInformationManagementInte TotalsCollector $totalsCollector, CartExtensionFactory $cartExtensionFactory = null, ShippingAssignmentFactory $shippingAssignmentFactory = null, - ShippingFactory $shippingFactory = null + ShippingFactory $shippingFactory = null, + ?AddressComparatorInterface $addressComparator = null, ) { $this->paymentMethodManagement = $paymentMethodManagement; $this->paymentDetailsFactory = $paymentDetailsFactory; @@ -146,6 +153,8 @@ class ShippingInformationManagement implements ShippingInformationManagementInte ->get(ShippingAssignmentFactory::class); $this->shippingFactory = $shippingFactory ?: ObjectManager::getInstance() ->get(ShippingFactory::class); + $this->addressComparator = $addressComparator + ?? ObjectManager::getInstance()->get(AddressComparatorInterface::class); } /** @@ -168,7 +177,7 @@ class ShippingInformationManagement implements ShippingInformationManagementInte $address = $addressInformation->getShippingAddress(); $this->validateAddress($address); - + $this->updateCustomerShippingAddressId($quote, $address); if (!$address->getCustomerAddressId()) { $address->setCustomerAddressId(null); } @@ -176,6 +185,7 @@ class ShippingInformationManagement implements ShippingInformationManagementInte try { $billingAddress = $addressInformation->getBillingAddress(); if ($billingAddress) { + $this->updateCustomerBillingAddressId($quote, $billingAddress); if (!$billingAddress->getCustomerAddressId()) { $billingAddress->setCustomerAddressId(null); } @@ -293,4 +303,39 @@ class ShippingInformationManagement implements ShippingInformationManagementInte $cartExtension->setShippingAssignments([$shippingAssignment]); return $quote->setExtensionAttributes($cartExtension); } + + /** + * Update customer shipping address ID if the address is the same as the quote shipping address. + * + * @param Quote $quote + * @param AddressInterface $address + * @return void + */ + private function updateCustomerShippingAddressId(Quote $quote, AddressInterface $address): void + { + $quoteShippingAddress = $quote->getShippingAddress(); + if (!$address->getCustomerAddressId() && + $quoteShippingAddress->getCustomerAddressId() && + $this->addressComparator->isEqual($address, $quoteShippingAddress) + ) { + $address->setCustomerAddressId($quoteShippingAddress->getCustomerAddressId()); + } + } + + /** + * Update customer billing address ID if the address is the same as the quote billing address. + * + * @param Quote $quote + * @param AddressInterface $billingAddress + * @return void + */ + private function updateCustomerBillingAddressId(Quote $quote, AddressInterface $billingAddress): void + { + $quoteBillingAddress = $quote->getBillingAddress(); + if ($quoteBillingAddress->getCustomerAddressId() && + $this->addressComparator->isEqual($billingAddress, $quoteBillingAddress) + ) { + $billingAddress->setCustomerAddressId($quoteBillingAddress->getCustomerAddressId()); + } + } } diff --git a/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php b/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php index 18525508321..35dee57678f 100644 --- a/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php +++ b/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php @@ -138,11 +138,11 @@ class InputParamsResolver $serviceMethodName ); $inputData = array_merge($inputData, $this->request->getParams()); + $inputData = $this->filterInputData($inputData); } else { $inputData = $this->request->getRequestData(); } - $inputData = $this->filterInputData($inputData); $this->validateParameters($serviceClassName, $serviceMethodName, array_keys($route->getParameters())); return $this->paramsOverrider->override($inputData, $route->getParameters());