Skip to content

Commit 22f54a4

Browse files
author
Oleksii Korshenko
authored
Merge pull request magento#1719 from magento-engcom/2.2-develop-prs
Public Pull Requests magento#12171 7691: address with saveInAddressBook 0 are still being added to the address book for new customers(backport to 2.2) by @RomaKis Fixed Public Issues magento#7691 address with saveInAddressBook 0 are still being added to the address book for new customers
2 parents d4e666f + 9d72569 commit 22f54a4

File tree

8 files changed

+213
-35
lines changed

8 files changed

+213
-35
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
475475
'email' => $quote->getCustomerEmail()
476476
]
477477
);
478+
$shippingAddress->setData('quote_address_id', $quote->getShippingAddress()->getId());
478479
$addresses[] = $shippingAddress;
479480
$order->setShippingAddress($shippingAddress);
480481
$order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
@@ -486,6 +487,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
486487
'email' => $quote->getCustomerEmail()
487488
]
488489
);
490+
$billingAddress->setData('quote_address_id', $quote->getBillingAddress()->getId());
489491
$addresses[] = $billingAddress;
490492
$order->setBillingAddress($billingAddress);
491493
$order->setAddresses($addresses);

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
namespace Magento\Quote\Test\Unit\Model;
88

99
use Magento\Framework\Exception\NoSuchEntityException;
10-
1110
use Magento\Quote\Model\CustomerManagement;
11+
use Magento\Sales\Api\Data\OrderAddressInterface;
1212

1313
/**
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -540,12 +540,12 @@ public function testSubmit()
540540
$shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
541541
$payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
542542
$baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
543-
$convertedBillingAddress = $this->createMock(\Magento\Sales\Api\Data\OrderAddressInterface::class);
544-
$convertedShippingAddress = $this->createMock(\Magento\Sales\Api\Data\OrderAddressInterface::class);
543+
$convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
544+
$convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
545545
$convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
546546
$convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
547547

548-
$addresses = [$convertedShippingAddress, $convertedBillingAddress];
548+
$addresses = [$convertedShipping, $convertedBilling];
549549
$quoteItems = [$quoteItem];
550550
$convertedItems = [$convertedQuoteItem];
551551

@@ -574,7 +574,7 @@ public function testSubmit()
574574
'email' => '[email protected]'
575575
]
576576
)
577-
->willReturn($convertedShippingAddress);
577+
->willReturn($convertedShipping);
578578
$this->quoteAddressToOrderAddress->expects($this->at(1))
579579
->method('convert')
580580
->with(
@@ -584,22 +584,27 @@ public function testSubmit()
584584
'email' => '[email protected]'
585585
]
586586
)
587-
->willReturn($convertedBillingAddress);
587+
->willReturn($convertedBilling);
588+
589+
$billingAddress->expects($this->once())->method('getId')->willReturn(4);
590+
$convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
588591
$this->quoteItemToOrderItem->expects($this->once())->method('convert')
589592
->with($quoteItem, ['parent_item' => null])
590593
->willReturn($convertedQuoteItem);
591594
$this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
592595
->willReturn($convertedPayment);
593596
$shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
597+
$shippingAddress->expects($this->once())->method('getId')->willReturn(5);
598+
$convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
594599

595600
$order = $this->prepareOrderFactory(
596601
$baseOrder,
597-
$convertedBillingAddress,
602+
$convertedBilling,
598603
$addresses,
599604
$convertedPayment,
600605
$convertedItems,
601606
$quoteId,
602-
$convertedShippingAddress
607+
$convertedShipping
603608
);
604609

605610
$this->orderManagement->expects($this->once())
@@ -973,9 +978,6 @@ protected function setPropertyValue(&$object, $property, $value)
973978
return $object;
974979
}
975980

976-
/**
977-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
978-
*/
979981
public function testSubmitForCustomer()
980982
{
981983
$orderData = [];
@@ -988,16 +990,12 @@ public function testSubmitForCustomer()
988990
$shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
989991
$payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
990992
$baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
991-
$convertedBillingAddress = $this->createMock(
992-
\Magento\Sales\Api\Data\OrderAddressInterface::class
993-
);
994-
$convertedShippingAddress = $this->createMock(
995-
\Magento\Sales\Api\Data\OrderAddressInterface::class
996-
);
993+
$convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
994+
$convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
997995
$convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
998996
$convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
999997

1000-
$addresses = [$convertedShippingAddress, $convertedBillingAddress];
998+
$addresses = [$convertedShipping, $convertedBilling];
1001999
$quoteItems = [$quoteItem];
10021000
$convertedItems = [$convertedQuoteItem];
10031001

@@ -1026,7 +1024,7 @@ public function testSubmitForCustomer()
10261024
'email' => '[email protected]'
10271025
]
10281026
)
1029-
->willReturn($convertedShippingAddress);
1027+
->willReturn($convertedShipping);
10301028
$this->quoteAddressToOrderAddress->expects($this->at(1))
10311029
->method('convert')
10321030
->with(
@@ -1036,22 +1034,24 @@ public function testSubmitForCustomer()
10361034
'email' => '[email protected]'
10371035
]
10381036
)
1039-
->willReturn($convertedBillingAddress);
1037+
->willReturn($convertedBilling);
10401038
$this->quoteItemToOrderItem->expects($this->once())->method('convert')
10411039
->with($quoteItem, ['parent_item' => null])
10421040
->willReturn($convertedQuoteItem);
10431041
$this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
10441042
->willReturn($convertedPayment);
10451043
$shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
1044+
$shippingAddress->expects($this->once())->method('getId')->willReturn(5);
1045+
$convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
10461046

10471047
$order = $this->prepareOrderFactory(
10481048
$baseOrder,
1049-
$convertedBillingAddress,
1049+
$convertedBilling,
10501050
$addresses,
10511051
$convertedPayment,
10521052
$convertedItems,
10531053
$quoteId,
1054-
$convertedShippingAddress
1054+
$convertedShipping
10551055
);
10561056
$customerAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
10571057
->getMockForAbstractClass();
@@ -1060,6 +1060,8 @@ public function testSubmitForCustomer()
10601060
$quote->expects($this->any())->method('addCustomerAddress')->with($customerAddressMock);
10611061
$billingAddress->expects($this->once())->method('getCustomerId')->willReturn(2);
10621062
$billingAddress->expects($this->once())->method('getSaveInAddressBook')->willReturn(false);
1063+
$billingAddress->expects($this->once())->method('getId')->willReturn(4);
1064+
$convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
10631065
$this->orderManagement->expects($this->once())
10641066
->method('place')
10651067
->with($order)
@@ -1073,4 +1075,25 @@ public function testSubmitForCustomer()
10731075
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
10741076
$this->assertEquals($order, $this->model->submit($quote, $orderData));
10751077
}
1078+
1079+
/**
1080+
* Get mock for abstract class with methods.
1081+
*
1082+
* @param string $className
1083+
* @param array $methods
1084+
*
1085+
* @return \PHPUnit_Framework_MockObject_MockObject
1086+
*/
1087+
private function createPartialMockForAbstractClass($className, $methods = [])
1088+
{
1089+
return $this->getMockForAbstractClass(
1090+
$className,
1091+
[],
1092+
'',
1093+
true,
1094+
true,
1095+
true,
1096+
$methods
1097+
);
1098+
}
10761099
}

app/code/Magento/Sales/Model/Order/CustomerManagement.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Sales\Model\Order;
78

9+
use Magento\Framework\App\ObjectManager;
810
use Magento\Framework\Exception\AlreadyExistsException;
911
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1012

@@ -43,28 +45,38 @@ class CustomerManagement implements \Magento\Sales\Api\OrderCustomerManagementIn
4345
*/
4446
protected $objectCopyService;
4547

48+
/**
49+
* @var \Magento\Quote\Model\Quote\AddressFactory
50+
*/
51+
private $quoteAddressFactory;
52+
4653
/**
4754
* @param \Magento\Framework\DataObject\Copy $objectCopyService
4855
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
4956
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory
5057
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory
5158
* @param \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory
5259
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
60+
* @param \Magento\Quote\Model\Quote\AddressFactory|null $quoteAddressFactory
5361
*/
5462
public function __construct(
5563
\Magento\Framework\DataObject\Copy $objectCopyService,
5664
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
5765
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory,
5866
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
5967
\Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory,
60-
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
68+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
69+
\Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory = null
6170
) {
6271
$this->objectCopyService = $objectCopyService;
6372
$this->accountManagement = $accountManagement;
6473
$this->orderRepository = $orderRepository;
6574
$this->customerFactory = $customerFactory;
6675
$this->addressFactory = $addressFactory;
6776
$this->regionFactory = $regionFactory;
77+
$this->quoteAddressFactory = $quoteAddressFactory ?: ObjectManager::getInstance()->get(
78+
\Magento\Quote\Model\Quote\AddressFactory::class
79+
);
6880
}
6981

7082
/**
@@ -84,6 +96,9 @@ public function create($orderId)
8496
);
8597
$addresses = $order->getAddresses();
8698
foreach ($addresses as $address) {
99+
if (!$this->isNeededToSaveAddress($address->getData('quote_address_id'))) {
100+
continue;
101+
}
87102
$addressData = $this->objectCopyService->copyFieldsetToTarget(
88103
'order_address',
89104
'to_customer_address',
@@ -117,6 +132,26 @@ public function create($orderId)
117132
$account = $this->accountManagement->createAccount($customer);
118133
$order->setCustomerId($account->getId());
119134
$this->orderRepository->save($order);
135+
120136
return $account;
121137
}
138+
139+
/**
140+
* Check if we need to save address in address book.
141+
*
142+
* @param int $quoteAddressId
143+
*
144+
* @return bool
145+
*/
146+
private function isNeededToSaveAddress($quoteAddressId)
147+
{
148+
$saveInAddressBook = true;
149+
150+
$quoteAddress = $this->quoteAddressFactory->create()->load($quoteAddressId);
151+
if ($quoteAddress && $quoteAddress->getId()) {
152+
$saveInAddressBook = (int)$quoteAddress->getData('save_in_address_book');
153+
}
154+
155+
return $saveInAddressBook;
156+
}
122157
}

0 commit comments

Comments
 (0)