Is their any way we can make customer lastname as optional while creating & saving customer,
also when we save customer address from $order->getBillingAddress()->getData()
?
code for reference -
$billing_adress = $order->getBillingAddress();
$store = $this->storeManager->getStore();
$storeId = $store->getStoreId();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($billing_adress->getEmail());
$customer_lastname = $billing_adress->getLastname();
if (!$customer->getId()) {
$password = bin2hex(openssl_random_pseudo_bytes(4));
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($billing_adress->getFirstname())
->setLastname(empty($customer_lastname) ? "." : $customer_lastname)
->setPrefix($billing_adress->getPrefix())
->setEmail($billing_adress->getEmail())
->setPassword($password);
$customer->save();
/* save billing Address */
$address = $this->addressFactory->create();
$address->setData($order->getBillingAddress()->getData());
$address->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('0')
->setSaveInAddressBook('1');
$address->save();
/* save shipping Address */
if (!$order->getIsVirtual()) {
$address = $this->addressFactory->create();
$address->setData($order->getShippingAddress()->getData());
$address->setCustomerId($customer->getId())
->setIsDefaultBilling('0')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
$address->save();
return $customer;
}
Would be great to hear your thoughts. Thanks