0

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

1 Answer 1

0
<?php

namespace Vendor\GuestStoreCredit\Setup\Patch\Data;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class CustomerLastnameOptional implements DataPatchInterface
{
    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }

    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['resourceConnection' => $this->moduleDataSetup]);
        $customerSetup->updateAttribute('customer', 'lastname', 'is_required', 0);
        $customerSetup->updateAttribute('customer_address', 'lastname', 'is_required', 0);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.