Skip to content

Commit c899815

Browse files
committed
added fill 'customerAddressId' for newly created customer
1 parent f8f1e5e commit c899815

File tree

3 files changed

+134
-13
lines changed

3 files changed

+134
-13
lines changed

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Quote\Model;
89

9-
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
1010
use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
1111
use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
12-
use Magento\Quote\Model\Quote as QuoteEntity;
12+
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
13+
use Magento\Customer\Model\AddressFactory;
1314
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\Validator\Exception as ValidatorException;
16+
use Magento\Framework\Validator\Factory as ValidatorFactory;
17+
use Magento\Quote\Model\Quote as QuoteEntity;
1418

1519
/**
1620
* Class Customer
@@ -33,12 +37,12 @@ class CustomerManagement
3337
protected $accountManagement;
3438

3539
/**
36-
* @var \Magento\Framework\Validator\Factory
40+
* @var ValidatorFactory
3741
*/
3842
private $validatorFactory;
3943

4044
/**
41-
* @var \Magento\Customer\Model\AddressFactory
45+
* @var AddressFactory
4246
*/
4347
private $addressFactory;
4448

@@ -47,23 +51,23 @@ class CustomerManagement
4751
* @param CustomerRepository $customerRepository
4852
* @param CustomerAddressRepository $customerAddressRepository
4953
* @param AccountManagement $accountManagement
50-
* @param \Magento\Framework\Validator\Factory|null $validatorFactory
51-
* @param \Magento\Customer\Model\AddressFactory|null $addressFactory
54+
* @param ValidatorFactory|null $validatorFactory
55+
* @param AddressFactory|null $addressFactory
5256
*/
5357
public function __construct(
5458
CustomerRepository $customerRepository,
5559
CustomerAddressRepository $customerAddressRepository,
5660
AccountManagement $accountManagement,
57-
\Magento\Framework\Validator\Factory $validatorFactory = null,
58-
\Magento\Customer\Model\AddressFactory $addressFactory = null
61+
ValidatorFactory $validatorFactory = null,
62+
AddressFactory $addressFactory = null
5963
) {
6064
$this->customerRepository = $customerRepository;
6165
$this->customerAddressRepository = $customerAddressRepository;
6266
$this->accountManagement = $accountManagement;
6367
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
64-
->get(\Magento\Framework\Validator\Factory::class);
68+
->get(ValidatorFactory::class);
6569
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
66-
->get(\Magento\Customer\Model\AddressFactory::class);
70+
->get(AddressFactory::class);
6771
}
6872

6973
/**
@@ -82,6 +86,7 @@ public function populateCustomerInfo(QuoteEntity $quote)
8286
$quote->getPasswordHash()
8387
);
8488
$quote->setCustomer($customer);
89+
$this->fillCustomerAddressId($quote);
8590
}
8691
if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
8792
$quote->getBillingAddress()->importCustomerAddressData(
@@ -100,11 +105,36 @@ public function populateCustomerInfo(QuoteEntity $quote)
100105
}
101106
}
102107

108+
/**
109+
* Filling 'CustomerAddressId' in quote for a newly created customer.
110+
*
111+
* @param QuoteEntity $quote
112+
* @return void
113+
*/
114+
private function fillCustomerAddressId(QuoteEntity $quote): void
115+
{
116+
$customer = $quote->getCustomer();
117+
118+
$customer->getDefaultBilling() ?
119+
$quote->getBillingAddress()->setCustomerAddressId($customer->getDefaultBilling()) :
120+
$quote->getBillingAddress()->setCustomerAddressId(0);
121+
122+
if ($customer->getDefaultShipping() || $customer->getDefaultBilling()) {
123+
if ($quote->getShippingAddress()->getSameAsBilling()) {
124+
$quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultBilling());
125+
} else {
126+
$quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultShipping());
127+
}
128+
} else {
129+
$quote->getShippingAddress()->setCustomerAddressId(0);
130+
}
131+
}
132+
103133
/**
104134
* Validate Quote Addresses
105135
*
106136
* @param Quote $quote
107-
* @throws \Magento\Framework\Validator\Exception
137+
* @throws ValidatorException
108138
* @return void
109139
*/
110140
public function validateAddresses(QuoteEntity $quote)
@@ -126,7 +156,7 @@ public function validateAddresses(QuoteEntity $quote)
126156
$addressModel = $this->addressFactory->create();
127157
$addressModel->updateData($address);
128158
if (!$validator->isValid($addressModel)) {
129-
throw new \Magento\Framework\Validator\Exception(
159+
throw new ValidatorException(
130160
null,
131161
null,
132162
$validator->getMessages()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected function setUp(): void
147147

148148
public function testPopulateCustomerInfo()
149149
{
150-
$this->quoteMock->expects($this->once())
150+
$this->quoteMock->expects($this->atLeastOnce())
151151
->method('getCustomer')
152152
->willReturn($this->customerMock);
153153
$this->customerMock->expects($this->atLeastOnce())
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @magentoDbIsolation disabled
20+
* @magentoAppArea adminhtml
21+
*/
22+
class CustomerManagementTest extends TestCase
23+
{
24+
/**
25+
* @var ObjectManagerInterface
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var CustomerManagement
31+
*/
32+
private $customerManagemet;
33+
34+
/**
35+
* @var CustomerInterface
36+
*/
37+
private $customer;
38+
39+
protected function setUp(): void
40+
{
41+
$this->objectManager = BootstrapHelper::getObjectManager();
42+
$this->customerManagemet = $this->objectManager->create(CustomerManagement::class);
43+
$this->customer = $this->objectManager->create(CustomerInterface::class);
44+
}
45+
46+
protected function tearDown(): void
47+
{
48+
/** @var CustomerRepositoryInterface $customerRepository */
49+
$customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
50+
$customer = $customerRepository->get('john1.doe001@test.com');
51+
$customerRepository->delete($customer);
52+
}
53+
54+
/**
55+
* @magentoDataFixture Magento/Sales/_files/quote.php
56+
*/
57+
public function testCustomerAddressIdQuote(): void
58+
{
59+
$reservedOrderId = 'test01';
60+
61+
$this->customer->setEmail('john1.doe001@test.com')
62+
->setFirstname('doe')
63+
->setLastname('john');
64+
65+
$quote = $this->getQuote($reservedOrderId)->setCustomer($this->customer);
66+
$this->customerManagemet->populateCustomerInfo($quote);
67+
self::assertNotNull($quote->getBillingAddress()->getCustomerAddressId());
68+
self::assertNotNull($quote->getShippingAddress()->getCustomerAddressId());
69+
}
70+
71+
/**
72+
* Gets quote by reserved order ID.
73+
*
74+
* @param string $reservedOrderId
75+
* @return Quote
76+
*/
77+
private function getQuote(string $reservedOrderId): Quote
78+
{
79+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
80+
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
81+
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
82+
->create();
83+
84+
/** @var CartRepositoryInterface $quoteRepository */
85+
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
86+
$items = $quoteRepository->getList($searchCriteria)
87+
->getItems();
88+
89+
return array_pop($items);
90+
}
91+
}

0 commit comments

Comments
 (0)