2

I want to add validation only for billing address on the checkout page.

By default in the backend, the company name is optional.

enter image description here

But if when I go to the checkout page and click on the Place Order button then I want to add require field validation for company field.

enter image description here

In which company name is not available so I just want to trigger that required.

Does have any idea about this?

2 Answers 2

2

Try this,

Add di.xml in your module

<?xml version="1.0" ?>                                                  
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Checkout\Block\Checkout\LayoutProcessor" type="vendor\module\Block\Checkout\LayoutProcessor" />

Create a LayoutProcessor.php in this path vendor\module\Block\Checkout\LayoutProcessor.php

Copy the file from

Vendor/magento/module-checkout/Block/Checkout/LayoutProcessor.php

To

app/code/vendor/module/Block/Checkout/LayoutProcessor.php

and change the below params to make the company as required field

 'company' => [
                            'validation' => [
                                'required-entry' => true,
                                'min_text_length' => 0,
                            ],
                        ],

Existing Billing Address of company field validation.

Add this file in app/code/vendor/module/etc/di.xml

add the following code,

<?xml version="1.0"?>                                           
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Checkout\Model\PaymentInformationManagement" type="Vendor\Module\Model\PaymentInformationManagement" />

Add the file in app/code/Vendor\Module\Model\PaymentInformationManagement.php

add the following code to validate empty company in billing address is not allowed.

<?php                                                         
 namespace Vendor\Module\Model;                                     
 use Magento\Framework\Exception\CouldNotSaveException;           
 class PaymentInformationManagement extends \Magento\Checkout\Model\PaymentInformationManagement                      
 {
public function savePaymentInformationAndPlaceOrder(
    $cartId,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
    $billingAddress = $cart->getQuote()->getBillingAddress();

    if (!$billingAddress->getCompany()) {
        throw new CouldNotSaveException(__('Company should not be empty in billing address'));
        return false;
    } else {
        $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
        try {
            $orderId = $this->cartManagement->placeOrder($cartId);
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __('An error occurred on the server. Please try to place the order again.'),
                $e
        );
        }
        return $orderId;
    }
}}

Hope this helps. Peace :)

8
0

Can you just update it to Required using the same Dropdown?

enter image description here

1

Your Answer

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

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