1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Model;
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
use Magento\Quote\Model\Quote as QuoteEntity;
use Magento\Framework\App\ObjectManager;
/**
* Class Customer
*/
class CustomerManagement
{
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* @var CustomerAddressRepository
*/
protected $customerAddressRepository;
/**
* @var AccountManagement
*/
protected $accountManagement;
/**
* @var \Magento\Framework\Validator\Factory
*/
private $validatorFactory;
/**
* @var \Magento\Customer\Model\AddressFactory
*/
private $addressFactory;
/**
* CustomerManagement constructor.
* @param CustomerRepository $customerRepository
* @param CustomerAddressRepository $customerAddressRepository
* @param AccountManagement $accountManagement
* @param \Magento\Framework\Validator\Factory|null $validatorFactory
* @param \Magento\Customer\Model\AddressFactory|null $addressFactory
*/
public function __construct(
CustomerRepository $customerRepository,
CustomerAddressRepository $customerAddressRepository,
AccountManagement $accountManagement,
\Magento\Framework\Validator\Factory $validatorFactory = null,
\Magento\Customer\Model\AddressFactory $addressFactory = null
) {
$this->customerRepository = $customerRepository;
$this->customerAddressRepository = $customerAddressRepository;
$this->accountManagement = $accountManagement;
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
->get(\Magento\Framework\Validator\Factory::class);
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
->get(\Magento\Customer\Model\AddressFactory::class);
}
/**
* Populate customer model
*
* @param Quote $quote
* @return void
*/
public function populateCustomerInfo(QuoteEntity $quote)
{
$customer = $quote->getCustomer();
if (!$customer->getId()) {
$customer = $this->accountManagement->createAccountWithPasswordHash(
$customer,
$quote->getPasswordHash()
);
$quote->setCustomer($customer);
}
if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
$quote->getBillingAddress()->importCustomerAddressData(
$this->customerAddressRepository->getById($customer->getDefaultBilling())
);
$quote->getBillingAddress()->setCustomerAddressId($customer->getDefaultBilling());
}
if (!$quote->getShippingAddress()->getSameAsBilling()
&& !$quote->getBillingAddress()->getId()
&& $customer->getDefaultShipping()
) {
$quote->getShippingAddress()->importCustomerAddressData(
$this->customerAddressRepository->getById($customer->getDefaultShipping())
);
$quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultShipping());
}
}
/**
* Validate Quote Addresses
*
* @param Quote $quote
* @throws \Magento\Framework\Validator\Exception
* @return void
*/
public function validateAddresses(QuoteEntity $quote)
{
$addresses = [];
if ($quote->getBillingAddress()->getCustomerAddressId()) {
$addresses[] = $this->customerAddressRepository->getById(
$quote->getBillingAddress()->getCustomerAddressId()
);
}
if ($quote->getShippingAddress()->getCustomerAddressId()) {
$addresses[] = $this->customerAddressRepository->getById(
$quote->getShippingAddress()->getCustomerAddressId()
);
}
if (!empty($addresses)) {
foreach ($addresses as $address) {
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
$addressModel = $this->addressFactory->create();
$addressModel->updateData($address);
if (!$validator->isValid($addressModel)) {
throw new \Magento\Framework\Validator\Exception(
null,
null,
$validator->getMessages()
);
}
}
}
}
}