SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH

Checkout Page Remove Last Name and Merge Last Name field value to First Name

On the checkout page, I want to remove the Last Name field and Merge the Last Name field value with the First Name field.

  1. Create a module.

app/code/Magepow/CustomFieldCheckout/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magepow_CustomFieldCheckout',
    __DIR__
);

app/code/Magepow/CustomFieldCheckout/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magepow_CustomFieldCheckout" setup_version="1.0.3"/>
</config>

app/code/Magepow/CustomFieldCheckout/Setup/UpgradeSchema.php

<?php
namespace Magepow\CustomFieldCheckout\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '&lt;=')) {
            $setup->startSetup();
            $setup->getConnection()->addColumn(
                $setup->getTable('customer_entity'),
                'lastname_bk',
                ['type'    => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => 0,
                    'comment'  => 'Last name field backup'
                ]
            );
            $setup->getConnection()->addColumn(
                $setup->getTable('customer_address_entity'),
                'lastname_bk',
                ['type'    => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => 0,
                    'comment'  => 'Last name field backup'
                ]
            );
            $setup->endSetup();
        }
    }
}

app/code/Magepow/CustomFieldCheckout/Setup/UpgradeData.php

<?php

namespace Magepow\CustomFieldCheckout\Setup;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\App\ResourceConnection;

class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    private $customerSetupFactory;
    protected $resourceConnection;

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

    public function upgrade(ModuleDataSetupInterface $setup,
                            ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.3', '<=')) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->updateAttribute(Customer::ENTITY, 'lastname', 'is_required', 0);
            $entityAttributes = [
                'customer_address' => [
                    'lastname' => [
                        'is_required' => 0,
                        'is_visible' => 0,
                    ],
                ],
            ];
            $customerSetup->upgradeAttributes($entityAttributes);

            $tableName = $this->getTableName('customer_entity');
            $sql = "update $tableName set lastname_bk = lastname";
            $this->resourceConnection->getConnection()->query($sql);
            $sql = "update $tableName set firstname = concat( firstname, ' ', lastname)";
            $this->resourceConnection->getConnection()->query($sql);
            $sql = "update $tableName set lastname = NULL";
            $this->resourceConnection->getConnection()->query($sql);

            $tableName = $this->getTableName('customer_address_entity');
            $sql = "update $tableName set lastname_bk = lastname";
            $this->resourceConnection->getConnection()->query($sql);
            $sql = "update $tableName set firstname = concat( firstname, ' ', lastname)";
            $this->resourceConnection->getConnection()->query($sql);
            $sql = "update $tableName set lastname = ''";
            $this->resourceConnection->getConnection()->query($sql);

        }
        $setup->endSetup();
    }

    public function getTablename($tableName)
    {
        /* Create Connection */
        $connection  = $this->resourceConnection->getConnection();
        $tableName   = $connection->getTableName($tableName);
        return $tableName;
    }
}

Using SSH execute command at the root of Magento 2 installation.

Using SSH execute command at the root of Magento 2 installation.

php -dmemory_limit=4G bin/magento setup:upgrade
php -dmemory_limit=4G bin/magento setup:static-content:deploy -f
php -dmemory_limit=4G bin/magento cache:flush

Result

Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feel free to contact us at Alothemes and

Phone: (+84)865633728

Email: support@alothemes.com