1

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

Nick
  • 11
  • 4
jibin george
  • 669
  • 3
  • 17

1 Answers1

1

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!

Kirti Nariya
  • 3,471
  • 2
  • 21
  • 50
  • I want the VAT number filed on to present when i click the user to zero vat customer group. How can i do that @kirti nariya – jibin george Nov 21 '18 at 11:55
  • It didnt work it just removes the Tax/Vat filed from all Groups. I wanted to show the field only when i chose Group = zero vat customer group – jibin george Nov 21 '18 at 12:03
  • @Jibin george you can check this link for hide / show filed in admin https://magento.stackexchange.com/questions/45819/how-to-show-and-hide-field-in-magento-admin-form-on-change-function – Kirti Nariya Nov 21 '18 at 13:02
  • That page didn't help much, I need to change the ui_component. at the the code you have provided here i tried that and that only hides the field for all groups i need to hide it just for one specific group – jibin george Nov 21 '18 at 13:30
  • @jibingeorge Please check my update my answer. – Kirti Nariya Nov 26 '18 at 16:39
  • Hi, thank you for the reply, i still cant get it working i am getting error for "Failed to load the "Vendor_CustomerForm/js/customer" component." require.js:1895 GET http://www.magento2.local/static/version1543312963/adminhtml/Magento/backend/en_US/Vendor_CustomerForm/js/customer.js net::ERR_ABORTED 404 (Not Found) the js file isn't getting picked up if i am right – jibin george Nov 27 '18 at 10:05
  • Please run command below four command and check. php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flush – Kirti Nariya Nov 27 '18 at 10:45
  • I have tested and working for me. After let me know if not work. – Kirti Nariya Nov 27 '18 at 10:45
  • It is working ? – Kirti Nariya Nov 27 '18 at 11:41
  • Hi @ Kirti Nariya thank you so much, it worked much appreciate really :) – jibin george Nov 27 '18 at 11:42
  • @jibingeorge Welcome. Happy Coding!! – Kirti Nariya Nov 27 '18 at 11:43
  • Kirti Nariya can i ask you a question, is it possible to show required filed for the taxvat field when i choose specific selected group like 1 and also show the taxvat filed specific selected group like 2 but not show the required part just show the field. – jibin george Nov 27 '18 at 16:54
  • @jibingeorge check my updated answer for required filed for the taxvat – Kirti Nariya Nov 28 '18 at 04:50
  • Hi Kirti Nariya, i have tried that and that does work but now value =1 is required and even if i put 2 values for if statement they both required. e .g if(value == 1 || value == 2) { taxvat.show(); } else { taxvat.hide(); } if i click it for the value 2 i want to show the filed but not required that was my question but thank you for this as well – jibin george Nov 28 '18 at 08:17