2

enter image description here

I want to add two more Labels Colony and Area after Street Address.How can I achieve this ?

0

I assume the 3 fields are address fields and you want to add labels on fields 2 and 3.
If so, you can achieve this via a layout processor.

First declare your layout processor.
Add this in one of your custom modules in etc/frontend/di.xml

<type name="Magento\Checkout\Block\Onepage">
    <arguments>
        <argument name="layoutProcessors" xsi:type="array">
            <item name="add-street-labels" xsi:type="object">[Namespace]\[Module]\Block\LayoutProcessor</item>
        </argument>
    </arguments>
</type>

Now create the layout processor [Namespace]/[Module]/Block/LayoutProcessor.php

<?php

namespace [Namespace]\[Module]\Block;

use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Magento\Framework\Stdlib\ArrayManager;

class LayoutProcessor implements LayoutProcessorInterface
{
    /**
     * @var ArrayManager
     */
    private $arrayManager;

    /**
     * LayoutProcessor constructor.
     * @param ArrayManager $arrayManager
     */
    public function __construct(ArrayManager $arrayManager) 
    {
         $this->arrayManager = $arrayManager;
    }    

    /**
     * @param array $jsLayout
     * @return array
     */
    public function process($jsLayout)
    {
        return $this->modifyAddressLabels($jsLayout);
    }

    /**
     * @param array $jsLayout
     * @return array
     */
    private function modifyAddressLabels($jsLayout)
    {
        //layout path of the street component
        $path = 'components/checkout/children/steps/children/shipping-step/children/shippingAddress/children/';
        $path .= 'shipping-address-fieldset/children/street/children';
        $line1Path = $path . '/1'; //line with index 1 (second field has index 1 because arrays start at 0)
        $line1 = $this->arrayManager->get($line1Path, $jsLayout); 
        if ($line1) {
            $line1['label'] = __('Colony');
            $jsLayout = $this->arrayManager->set($line1Path, $jsLayout, $line1);
        }

        $line2Path = $path . '/2'; //line with index 2
        $line2 = $this->arrayManager->get($line2Path, $jsLayout); 
        if ($line2) {
            $line2['label'] = __('Area');
            $jsLayout = $this->arrayManager->set($line2Path, $jsLayout, $line2);
        }
        return $jsLayout;
    }
}

You can even skip the modifyAddressLabels method and implement everything in process methods. I did it like this in case you need to add other changes to the layout and you want to keep them separate.
Untested code so watch out for typos.

  • Thanks For the answer. I tried it but didn't work. Also I want this for Customer->Add New Customer->Address. – Kaushal Dec 2 '19 at 11:00

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.