Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Jun 6, 2024
2 parents abb184c + 001e518 commit 1374fb1
Show file tree
Hide file tree
Showing 54 changed files with 1,417 additions and 604 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Checkout/Model/AddressComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function isEqual(?AddressInterface $address1, ?AddressInterface $address2
(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);
Expand Down
72 changes: 57 additions & 15 deletions app/code/Magento/Checkout/Model/PaymentInformationManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
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;

/**
Expand Down Expand Up @@ -137,8 +141,8 @@ public function __construct(
*/
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 {
Expand Down Expand Up @@ -180,8 +184,8 @@ public function savePaymentInformationAndPlaceOrder(
*/
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 {
Expand All @@ -201,6 +205,7 @@ public function savePaymentInformation(
//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);
Expand Down Expand Up @@ -245,18 +250,12 @@ private function processShippingAddress(Quote $quote): void
$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);
Expand All @@ -265,4 +264,47 @@ private function processShippingAddress(Quote $quote): void
$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);
}
}
}
}
49 changes: 47 additions & 2 deletions app/code/Magento/Checkout/Model/ShippingInformationManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class ShippingInformationManagement implements ShippingInformationManagementInte
*/
private $shippingFactory;

/**
* @var AddressComparatorInterface
*/
private $addressComparator;

/**
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentDetailsFactory $paymentDetailsFactory
Expand All @@ -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(
Expand All @@ -129,7 +135,8 @@ public function __construct(
TotalsCollector $totalsCollector,
CartExtensionFactory $cartExtensionFactory = null,
ShippingAssignmentFactory $shippingAssignmentFactory = null,
ShippingFactory $shippingFactory = null
ShippingFactory $shippingFactory = null,
?AddressComparatorInterface $addressComparator = null,
) {
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentDetailsFactory = $paymentDetailsFactory;
Expand All @@ -146,6 +153,8 @@ public function __construct(
->get(ShippingAssignmentFactory::class);
$this->shippingFactory = $shippingFactory ?: ObjectManager::getInstance()
->get(ShippingFactory::class);
$this->addressComparator = $addressComparator
?? ObjectManager::getInstance()->get(AddressComparatorInterface::class);
}

/**
Expand All @@ -168,14 +177,15 @@ public function saveAddressInformation(

$address = $addressInformation->getShippingAddress();
$this->validateAddress($address);

$this->updateCustomerShippingAddressId($quote, $address);
if (!$address->getCustomerAddressId()) {
$address->setCustomerAddressId(null);
}

try {
$billingAddress = $addressInformation->getBillingAddress();
if ($billingAddress) {
$this->updateCustomerBillingAddressId($quote, $billingAddress);
if (!$billingAddress->getCustomerAddressId()) {
$billingAddress->setCustomerAddressId(null);
}
Expand Down Expand Up @@ -293,4 +303,39 @@ private function prepareShippingAssignment(
$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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Magento\Checkout\Api\PaymentProcessingRateLimiterInterface;
use Magento\Checkout\Api\PaymentSavingRateLimiterInterface;
use Magento\Checkout\Model\PaymentInformationManagement;
use Magento\Customer\Api\Data\AddressInterface as CustomerAddressInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
Expand Down Expand Up @@ -72,6 +74,11 @@ class PaymentInformationManagementTest extends TestCase
*/
private $saveLimiterMock;

/**
* @var Address|MockObject
*/
private $quoteShippingAddress;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
Expand Down Expand Up @@ -100,10 +107,33 @@ protected function setUp(): void
);
$objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
$objectManager->setBackwardCompatibleProperty($this->model, 'cartRepository', $this->cartRepositoryMock);

$this->quoteShippingAddress = $this->getMockBuilder(Address::class)
->addMethods(['setLimitCarrier'])
->onlyMethods([
'getShippingMethod',
'getShippingRateByCode',
'getSameAsBilling',
'getSaveInAddressBook',
'setSaveInAddressBook',
'exportCustomerAddress'])
->disableOriginalConstructor()
->getMock();
}

public function testSavePaymentInformationAndPlaceOrder()
{
$shippingAddressMock = $this->createMock(CustomerAddressInterface::class);
$this->quoteShippingAddress->expects($this->once())
->method('getSaveInAddressBook')
->willReturn(true);
$this->quoteShippingAddress->expects($this->once())
->method('getSameAsBilling')
->willReturn(true);
$this->quoteShippingAddress->expects($this->once())
->method('exportCustomerAddress')
->willReturn($shippingAddressMock);

$orderId = 200;
$this->assertEquals(
$orderId,
Expand Down Expand Up @@ -239,25 +269,29 @@ private function getMockForAssignBillingAddress($cartId, $billingAddressMock)
{
$billingAddressId = 1;
$quoteMock = $this->createMock(Quote::class);
$customerMock = $this->createMock(CustomerInterface::class);
$quoteBillingAddress = $this->createMock(Address::class);
$shippingRate = $this->createPartialMock(Rate::class, []);
$shippingRate->setCarrier('flatrate');
$quoteShippingAddress = $this->getMockBuilder(Address::class)
->addMethods(['setLimitCarrier'])
->onlyMethods(['getShippingMethod', 'getShippingRateByCode'])
->disableOriginalConstructor()
->getMock();
$this->cartRepositoryMock->expects($this->any())->method('getActive')->with($cartId)->willReturn($quoteMock);
$quoteMock->method('getBillingAddress')->willReturn($quoteBillingAddress);
$quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($quoteShippingAddress);
$quoteMock->method('getCustomer')->willReturn($customerMock);
$quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($this->quoteShippingAddress);
$quoteBillingAddress->expects($this->any())->method('getId')->willReturn($billingAddressId);
$quoteBillingAddress->expects($this->any())->method('getId')->willReturn($billingAddressId);
$quoteMock->expects($this->any())->method('removeAddress')->with($billingAddressId);
$quoteMock->expects($this->any())->method('setBillingAddress')->with($billingAddressMock);
$quoteMock->expects($this->any())->method('setDataChanges')->willReturnSelf();
$quoteShippingAddress->expects($this->any())->method('getShippingRateByCode')->willReturn($shippingRate);
$quoteShippingAddress->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate');
$quoteShippingAddress->expects($this->any())->method('setLimitCarrier')->with('flatrate')->willReturnSelf();
$this->quoteShippingAddress->expects($this->any())
->method('getShippingRateByCode')
->willReturn($shippingRate);
$this->quoteShippingAddress->expects($this->any())
->method('getShippingMethod')
->willReturn('flatrate_flatrate');
$this->quoteShippingAddress->expects($this->any())
->method('setLimitCarrier')
->with('flatrate')
->willReturnSelf();
}

/**
Expand Down
Loading

0 comments on commit 1374fb1

Please sign in to comment.