1

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.

3 Answers 3

3

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>
2
  • i have to save it as {Vendorname}\{Module}\Plugin\Block\Magento\Checkout\LayoutProcessorPlugin.php?
    – user48824
    Apr 3, 2018 at 18:53
  • sorry man it do not work
    – user48824
    Apr 4, 2018 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

1
  • I would prefer something like: UPDATE eav_attribute SET is_require = '1' WHERE attribute_code='vat_id';
    – jrosell
    Jun 16, 2020 at 12:36
0
UPDATE eav_attribute SET is_required = '1'
WHERE eav_attribute.attribute_code = 'vat_id'
  AND entity_type_id = 2;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.