Skip to content

Add custom customer attribute for default magento tax class

I have one requirement to create one custom customer attribute. That attribute should appear in the New tab in customer edit page in Admin layout customer_index_edit. Currently I am facing an issue like that attribute is appearing inside Account Information tab only, I want that inside the New Tab and also its value is not being save from admin. Also, that customer custom attribute value consists of Default Magento Tax Class for Customer. Below is my code,

<?php
namespace AbcCustomCustomerTaxClassSetup;

use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupInstallDataInterface;

class InstallData implements InstallDataInterface
{
    private $customerSetupFactory;

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

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

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        // Adding custom attribute for customer tax class
        $customerSetup->addAttribute(
            MagentoCustomerModelCustomer::ENTITY,
            'custom_cust_tax_class',
            [
                'type' => 'int',
                'label' => 'Custom Customer Tax Class',
                'input' => 'select',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'position' => 500,
                'system' => 0,
                'source' => 'MagentoTaxModelTaxClassSourceCustomer',
                'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
            ]
        );

        // Add attribute to customer default attribute set and group
        $attributeSetId = $customerSetup->getDefaultAttributeSetId(MagentoCustomerModelCustomer::ENTITY);
        $attributeGroupId = $customerSetup->getDefaultAttributeGroupId(MagentoCustomerModelCustomer::ENTITY, $attributeSetId);
        
        // Add attribute to the group
        $customerSetup->addAttributeToSet(
            MagentoCustomerModelCustomer::ENTITY,
            $attributeSetId,
            $attributeGroupId,
            'custom_cust_tax_class'
        );

        // Make the attribute visible on the admin form
        $attribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'custom_cust_tax_class');
        $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer'] // Ensures it is shown on the admin customer form
        );
        $attribute->save();

        $setup->endSetup();
    }
}

For saving the value, here is my di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoCustomerApiCustomerRepositoryInterface">
        <plugin name="custom_customer_tax_class_save_plugin" type="AbcCustomCustomerTaxClassPluginCustomerSavePlugin"/>
    </type>
</config>

Plugin File AbcCustomCustomerTaxClassPluginCustomerSavePlugin

<?php
namespace AbcCustomCustomerTaxClassPlugin;

use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoCustomerApiDataCustomerInterface;

class CustomerSavePlugin
{
    public function aroundSave(CustomerRepositoryInterface $subject, callable $proceed, CustomerInterface $customer)
    {
        if ($customer->getCustomAttribute('custom_cust_tax_class')) {
            $value = $customer->getCustomAttribute('custom_cust_tax_class')->getValue();
            $customer->setCustomAttribute('custom_cust_tax_class', $value);
        }

        return $proceed($customer);
    }
}

Here is my layout file

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_edit"/>
    <referenceBlock name="customer_edit_tabs">
        <block class="AbcCustomCustomerTaxClassBlockAdminhtmlCustomerEditTabTaxClass" name="custom_customer_tax_class_tab">
            <arguments>
                <argument name="data" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Custom Customer Tax Class</item>
                    <item name="sort_order" xsi:type="number">300</item>
                </argument>
            </arguments>
        </block>
    </referenceBlock>
</layout>

Any help is appreciated, Thank you in Advance !!!