0

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 Abc\CustomCustomerTaxClass\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;

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(
            \Magento\Customer\Model\Customer::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' => 'Magento\Tax\Model\TaxClass\Source\Customer',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            ]
        );

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

        // Make the attribute visible on the admin form
        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::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="Magento\Customer\Api\CustomerRepositoryInterface">
        <plugin name="custom_customer_tax_class_save_plugin" type="Abc\CustomCustomerTaxClass\Plugin\CustomerSavePlugin"/>
    </type>
</config>

Plugin File Abc\CustomCustomerTaxClass\Plugin\CustomerSavePlugin

<?php
namespace Abc\CustomCustomerTaxClass\Plugin;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;

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="Abc\CustomCustomerTaxClass\Block\Adminhtml\Customer\Edit\Tab\TaxClass" 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 !!!

0

Your Answer

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