Context: How does Magento 2 propose a guest buyer to register himself in the store on an order placement?
public function create($orderId)
{
$order = $this->orderRepository->get($orderId);
if ($order->getCustomerId()) {
throw new AlreadyExistsException(__("This order already has associated customer account"));
}
$customerData = $this->objectCopyService->copyFieldsetToTarget(
'order_address',
'to_customer',
$order->getBillingAddress(),
[]
);
Details: How does \Magento\Framework\DataObject\Copy::copyFieldsetToTarget()
work?
See also: How is «to_customer
» aspect used for a fieldset copying?
$addresses = $order->getAddresses();
foreach ($addresses as $address) {
$addressData = $this->objectCopyService->copyFieldsetToTarget(
'order_address',
'to_customer_address',
$address,
[]
);
/** @var \Magento\Customer\Api\Data\AddressInterface $customerAddress */
$customerAddress = $this->addressFactory->create(['data' => $addressData]);
switch ($address->getAddressType()) {
case QuoteAddress::ADDRESS_TYPE_BILLING:
$customerAddress->setIsDefaultBilling(true);
break;
case QuoteAddress::ADDRESS_TYPE_SHIPPING:
$customerAddress->setIsDefaultShipping(true);
break;
}
if (is_string($address->getRegion())) {
/** @var \Magento\Customer\Api\Data\RegionInterface $region */
$region = $this->regionFactory->create();
$region->setRegion($address->getRegion());
$region->setRegionCode($address->getRegionCode());
$region->setRegionId($address->getRegionId());
$customerAddress->setRegion($region);
}
$customerData['addresses'][] = $customerAddress;
}
Instead of loading a new page for creating a customer account for guest , can’t we display the registration form on success page by taking values from checkout page?