Question

enter image description here

I want the VAT number filed on to present when i click the user to zero vat customer group.If click any other group i dont want to show the vat field. Anyone has any idea how to fix this.

/private/var/www/html/magento2/vendor/magento/module-customer/view/base/ui_component/customer_form.xml

this is the core file for this

Was it helpful?

Solution

Hide Tax/Vat Number Field from customer admin form based on Customer Group.

You can hide Tax/Vat Number field and only show based on specific selected group like 1

You can change group value as you want show/hide on Tax/Vat Number field.

Follow below steps:

File path: magento/app/code/Vendor/CustomerForm/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CustomerForm',
    __DIR__
);

File path: magento/app/code/Vendor/CustomerForm/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="Vendor_CustomerForm" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

File path: magento/app/code/Vendor/CustomerForm/view/adminhtml/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">    
    <fieldset name="customer">
        <settings>
            <label translate="true">Account Information</label>
        </settings>
        <container name="container_group" component="Magento_Ui/js/form/components/group" sortOrder="20">
            <argument name="data" xsi:type="array">
                <item name="type" xsi:type="string">group</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Group</item>
                    <item name="required" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="boolean">false</item>
                    <item name="validateWholeGroup" xsi:type="boolean">true</item>
                </item>
            </argument>
            <field name="group_id" formElement="select">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="fieldGroup" xsi:type="string">group_id</item>
                        <item name="component" xsi:type="string">Vendor_CustomerForm/js/customer</item>
                        <item name="source" xsi:type="string">customer</item>
                    </item>
                </argument>
                <settings>
                    <dataType>number</dataType>
                </settings>
            </field>
        </container>
        <field name="taxvat" formElement="input">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">customer</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <visible>true</visible>
        </settings>
    </field>
    </fieldset>
</form>

File path: magento/app/code/Vendor/CustomerForm/view/adminhtml/web/js/customer.js

define([
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
    'use strict';

    return select.extend({
        onUpdate: function (value) {

            var taxvat = uiRegistry.get('index = taxvat');
            if(value == 1) {
                taxvat.show();
            } else {
                taxvat.hide();
            }
            return this._super();
        },
    });
});

Try this above code and let me know if any issue.

Hope it help!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top