0

How can I make the vat tax required in the Magento-2 checkout? From the panel, I can set it as default only in the "create new account" form.

2

You can achieve via create plugin of class \Magento\Checkout\Block\Checkout\LayoutProcessor

On after method of function process(), add required field to vat id field

PLugin class

<?php
namespace {Vendorname}\{Module}\Plugin\Block\Magento\Checkout;


class LayoutProcessorPlugin
{
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    )
    {

        if(isset(
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['vat_id']
        )){
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['vat_id']
            ['validation'] =[
                'required-entry' => true,
            ];
        }


        $configuration = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];
        foreach ($configuration as $paymentGroup => $groupConfig) {
            if (isset($groupConfig['component']) AND $groupConfig['component'] === 'Magento_Checkout/js/view/billing-address') {



                if(isset(
                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']
                    ['children']['vat_id']
                )){
                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']
                    ['children']['vat_id'] ['validation'] =[
                        'required-entry' => true,
                    ];
                }

            }
        }

        return $jsLayout;
    }

}

Define di.xml

<?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\Checkout\Block\Checkout\LayoutProcessor" >
        <plugin name="add_required_field_to_vat_id" sortOrder="1"
                type="{Vendorname}\{Module}\Plugin\Block\Magento\Checkout\LayoutProcessorPlugin"/>
    </type>
</config>
| improve this answer | |
  • i have to save it as {Vendorname}\{Module}\Plugin\Block\Magento\Checkout\LayoutProcessorPlugin.php? – user48824 Apr 3 '18 at 18:53
  • sorry man it do not work – user48824 Apr 4 '18 at 9:54
0

Run below SQL query

UPDATE `eav_attribute` SET `is_required` = '1' WHERE `eav_attribute`.`attribute_id` = 36;

enter image description here

| improve this answer | |
  • I would prefer something like: UPDATE eav_attribute SET is_require = '1' WHERE attribute_code='vat_id'; – jrosell Jun 16 at 12:36

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.