Question

We are currently building a Magento 2.1 shop for B2B

I want to require the vat for the checkout. I have this already fixed for the shipping fields, but not for the billing fields. This is necessary because One Page sometimes only showed the 'update payment information' and not the shipping fields. On that moment it's possible to place an order without a valid VAT.

I tried to set the vat_id to required in the database, but this has some nasty results with account registration and changing. In the backend, is vat set on required.

My checkout_index_index.xml is as follow:

<referenceBlock name="checkout.root">
        <arguments>
            <argument name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="checkout" xsi:type="array">
                        <item name="children" xsi:type="array">
                            <item name="steps" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="shipping-step" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="shippingAddress" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <!-- The name of the form the field belongs to -->
                                                    <item name="shipping-address-fieldset" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="vat_id" xsi:type="array">
                                                                <item name="validation" xsi:type="array">
                                                                    <item name="required-entry" xsi:type="boolean">true</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>

Another problem, that i have with the vat, is after checkout. When i place successfully an order (with valid VAT), i cannot create an account with the message VAT ID is not valid. This is a minor problem, but it would be better when i can fixed this to.

Was it helpful?

Solution

I fixed this by add an observer and change the jsLayout for for the form.

public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $subject, array $jsLayout)
{
    $elements = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
    ['payment']['children']['payments-list']['children'];

    if (isset($elements)) {
        foreach ($elements as $key => $value) {
            if (isset($value['children']['form-fields']['children'])) {
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']['vat_id']['validation']['required-entry'] = true;
            }
        }
    }
    return $jsLayout;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top