Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
CustomerManagement Class Reference

Public Member Functions

 __construct (CustomerRepository $customerRepository, CustomerAddressRepository $customerAddressRepository, AccountManagement $accountManagement, \Magento\Framework\Validator\Factory $validatorFactory=null, \Magento\Customer\Model\AddressFactory $addressFactory=null)
 
 populateCustomerInfo (QuoteEntity $quote)
 
 validateAddresses (QuoteEntity $quote)
 

Protected Attributes

 $customerRepository
 
 $customerAddressRepository
 
 $accountManagement
 

Detailed Description

Class Customer

Definition at line 18 of file CustomerManagement.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CustomerRepository  $customerRepository,
CustomerAddressRepository  $customerAddressRepository,
AccountManagement  $accountManagement,
\Magento\Framework\Validator\Factory  $validatorFactory = null,
\Magento\Customer\Model\AddressFactory  $addressFactory = null 
)

CustomerManagement constructor.

Parameters
CustomerRepository$customerRepository
CustomerAddressRepository$customerAddressRepository
AccountManagement$accountManagement
\Magento\Framework\Validator\Factory | null$validatorFactory
\Magento\Customer\Model\AddressFactory | null$addressFactory

Definition at line 53 of file CustomerManagement.php.

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  }
$addressFactory
Definition: quote.php:20

Member Function Documentation

◆ populateCustomerInfo()

populateCustomerInfo ( QuoteEntity  $quote)

Populate customer model

Parameters
Quote$quote
Returns
void

Definition at line 75 of file CustomerManagement.php.

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  }
$customer
Definition: customers.php:11
$quote

◆ validateAddresses()

validateAddresses ( QuoteEntity  $quote)

Validate Quote Addresses

Parameters
Quote$quote
Exceptions

Definition at line 110 of file CustomerManagement.php.

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  }
$addresses
Definition: address_list.php:7
$quote
$address
Definition: customer.php:38

Field Documentation

◆ $accountManagement

$accountManagement
protected

Definition at line 33 of file CustomerManagement.php.

◆ $customerAddressRepository

$customerAddressRepository
protected

Definition at line 28 of file CustomerManagement.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 23 of file CustomerManagement.php.


The documentation for this class was generated from the following file: