Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Quote\Model;
8 
11 use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
14 
19 {
24 
29 
33  protected $accountManagement;
34 
38  private $validatorFactory;
39 
43  private $addressFactory;
44 
53  public function __construct(
54  CustomerRepository $customerRepository,
55  CustomerAddressRepository $customerAddressRepository,
56  AccountManagement $accountManagement,
57  \Magento\Framework\Validator\Factory $validatorFactory = null,
58  \Magento\Customer\Model\AddressFactory $addressFactory = null
59  ) {
60  $this->customerRepository = $customerRepository;
61  $this->customerAddressRepository = $customerAddressRepository;
62  $this->accountManagement = $accountManagement;
63  $this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
64  ->get(\Magento\Framework\Validator\Factory::class);
65  $this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
66  ->get(\Magento\Customer\Model\AddressFactory::class);
67  }
68 
76  {
77  $customer = $quote->getCustomer();
78 
79  if (!$customer->getId()) {
80  $customer = $this->accountManagement->createAccountWithPasswordHash(
81  $customer,
82  $quote->getPasswordHash()
83  );
84  $quote->setCustomer($customer);
85  }
86  if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
87  $quote->getBillingAddress()->importCustomerAddressData(
88  $this->customerAddressRepository->getById($customer->getDefaultBilling())
89  );
90  $quote->getBillingAddress()->setCustomerAddressId($customer->getDefaultBilling());
91  }
92  if (!$quote->getShippingAddress()->getSameAsBilling()
93  && !$quote->getBillingAddress()->getId()
94  && $customer->getDefaultShipping()
95  ) {
96  $quote->getShippingAddress()->importCustomerAddressData(
97  $this->customerAddressRepository->getById($customer->getDefaultShipping())
98  );
99  $quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultShipping());
100  }
101  }
102 
111  {
112  $addresses = [];
113  if ($quote->getBillingAddress()->getCustomerAddressId()) {
114  $addresses[] = $this->customerAddressRepository->getById(
115  $quote->getBillingAddress()->getCustomerAddressId()
116  );
117  }
118  if ($quote->getShippingAddress()->getCustomerAddressId()) {
119  $addresses[] = $this->customerAddressRepository->getById(
120  $quote->getShippingAddress()->getCustomerAddressId()
121  );
122  }
123  if (!empty($addresses)) {
124  foreach ($addresses as $address) {
125  $validator = $this->validatorFactory->createValidator('customer_address', 'save');
126  $addressModel = $this->addressFactory->create();
127  $addressModel->updateData($address);
128  if (!$validator->isValid($addressModel)) {
129  throw new \Magento\Framework\Validator\Exception(
130  null,
131  null,
132  $validator->getMessages()
133  );
134  }
135  }
136  }
137  }
138 }
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
$quote
$address
Definition: customer.php:38
__construct(CustomerRepository $customerRepository, CustomerAddressRepository $customerAddressRepository, AccountManagement $accountManagement, \Magento\Framework\Validator\Factory $validatorFactory=null, \Magento\Customer\Model\AddressFactory $addressFactory=null)