CustomerManagement.php 4.7 KB

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