How To Remove Company Field From Checkout by programmatically in Magento 2.x

The following below custom module code snippet need to follow

Step [1] – Create Custom module, as we have already explained previous topic follow below link

Magento 2.x Custom Module Steps

Step [2] – Once custom module files registration.php & module.xml created, we need to create di.xml & LayoutProcessor.php

Step [3] – Create di.xml

File Path=app/code/Mage2db/John/etc/di.xml

Add below content in this file.

<?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="remove_company_from_checkout"
            type="Mage2db\John\Plugin\Block\Checkout\LayoutProcessor" sortOrder="10"/>
    </type>
</config>

Step [4] – Create LayoutProcessor.php

File Path=app/code/Mage2db/John/Plugin/Block/Checkout/LayoutProcessor.php

Add below content in this file.

<?php
namespace Mage2db\John\Plugin;
class LayoutProcessor
{
    /**
     * Process js Layout of block
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['company']['visible'] = 0;

        return $jsLayout;
    }
}

Step [5] – Finally your custom module to remove company field from checkout page has been created

Run the following below commands at your Magento 2 root directory

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush

Leave a Reply

Your email address will not be published. Required fields are marked *