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
8 
14 use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
16 
23 {
27  protected $accountManagement;
28 
32  protected $customerFactory;
33 
37  protected $addressFactory;
38 
42  protected $regionFactory;
43 
47  protected $orderRepository;
48 
52  protected $objectCopyService;
53 
57  private $quoteAddressFactory;
58 
62  private $customerExtractor;
63 
74  public function __construct(
76  \Magento\Customer\Api\AccountManagementInterface $accountManagement,
77  \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory,
78  \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
79  \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory,
81  QuoteAddressFactory $quoteAddressFactory = null,
82  OrderCustomerExtractor $orderCustomerExtractor = null
83  ) {
84  $this->objectCopyService = $objectCopyService;
85  $this->accountManagement = $accountManagement;
86  $this->orderRepository = $orderRepository;
87  $this->customerFactory = $customerFactory;
88  $this->addressFactory = $addressFactory;
89  $this->regionFactory = $regionFactory;
90  $this->quoteAddressFactory = $quoteAddressFactory
91  ?: ObjectManager::getInstance()->get(QuoteAddressFactory::class);
92  $this->customerExtractor = $orderCustomerExtractor
93  ?? ObjectManager::getInstance()->get(OrderCustomerExtractor::class);
94  }
95 
99  public function create($orderId)
100  {
101  $order = $this->orderRepository->get($orderId);
102  if ($order->getCustomerId()) {
103  throw new AlreadyExistsException(
104  __('This order already has associated customer account')
105  );
106  }
107 
108  $customer = $this->customerExtractor->extract($orderId);
110  $filteredAddresses = [];
111  foreach ($customer->getAddresses() as $address) {
112  if ($this->needToSaveAddress($order, $address)) {
113  $filteredAddresses[] = $address;
114  }
115  }
116  $customer->setAddresses($filteredAddresses);
117 
118  $account = $this->accountManagement->createAccount($customer);
119  $order = $this->orderRepository->get($orderId);
120  $order->setCustomerId($account->getId());
121  $order->setCustomerIsGuest(0);
122  $this->orderRepository->save($order);
123 
124  return $account;
125  }
126 
133  private function needToSaveAddress(
134  OrderInterface $order,
135  AddressInterface $address
136  ): bool {
138  $orderAddress = null;
139  if ($address->isDefaultBilling()) {
140  $orderAddress = $order->getBillingAddress();
141  } elseif ($address->isDefaultShipping()) {
142  $orderAddress = $order->getShippingAddress();
143  }
144  if ($orderAddress) {
145  $quoteAddressId = $orderAddress->getData('quote_address_id');
146  if ($quoteAddressId) {
148  $quote = $this->quoteAddressFactory->create()
149  ->load($quoteAddressId);
150  if ($quote && $quote->getId()) {
151  return (bool)(int)$quote->getData('save_in_address_book');
152  }
153  }
154 
155  return true;
156  }
157 
158  return false;
159  }
160 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
__construct(\Magento\Framework\DataObject\Copy $objectCopyService, \Magento\Customer\Api\AccountManagementInterface $accountManagement, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory, \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory, \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, QuoteAddressFactory $quoteAddressFactory=null, OrderCustomerExtractor $orderCustomerExtractor=null)
$quote
$order
Definition: order.php:55
__()
Definition: __.php:13
$address
Definition: customer.php:38