1

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

I have checked Magento Stack Exchange answer, but it removes the field from Registration and edit address page.

enter image description here

  • 1
    in magento 2 Last name is required field so if you want merge First name to Last Name you can hide Last name field and use js to fill Last Name. – xanka Mar 12 at 6:11
  • @Kushal did you complete this? – Hieu Duong Jun 16 at 3:07
0

app/code/[CompanyName]/[PackageName]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[CompanyName]_[PackageName]',
    __DIR__
);

app/code/[CompanyName]/[PackageName]/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="[CompanyName]_[PackageName]" setup_version="1.0.3"/>
</config>

app/code/[CompanyName]/[PackageName]/Setup/UpgradeSchema.php

<?php
namespace [CompanyName]\[PackageName]\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', '<=')) {
            $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/[CompanyName]/[PackageName]/Setup/UpgradeData.php

<?php

namespace [CompanyName]\[PackageName]\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;
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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