0

I want to change the Zip/Postal Code input field to dropdown in magento 2 chckout page.

Also I want to change its label.

I want the above to be done using a custom module.

This is what I did:

I created Custom Module.

etc/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="Custom_Checkout" type="SuperPrice\Postcodedropdown\Block\LayoutProcessor" sortOrder="100"/>
</type> 
</config>

Vendor/Module/Block/LayoutProcessor.php

<?php

namespace Vendor\Module\Block;


/**
* Class LayoutProcessor
*/
class LayoutProcessor
{

/**
 * @var AttributeMerger
 */
protected $merger;


/**
 * @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
 * @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
 * @param AttributeMerger $merger
 */
public function __construct(
    \Magento\Checkout\Block\Checkout\AttributeMerger $merger
) {
    $this->merger = $merger;
}

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']['postcode']['label'] = __('City'); 

    return $jsLayout;
}



public function process($jsLayout)
{
    $elements = [
        'city' => [
            'visible' => $this->isCityActive(),
            'formElement' => 'input',
            'label' => __('City'),
            'value' =>  null
        ],
        'country_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('Country'),
            'options' => [],
            'value' => null
        ],
        'region_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('State/Province'),
            'options' => [],
            'value' => null
        ],
        'postcode' => [
            'formElement' => 'select',
        'label' => __(' My City'),
        'options' => [
                [
                    'value' => '',
                    'label' => 'Please Select',
                ],
                [
                    'value' => '1',
                    'label' => 'First Option',
                ]
        ],
        'value' =>  null
        ]
    ];

        $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
        ['children']['address-fieldsets']['children'];
        $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);

    return $jsLayout;
}

}

No effects for code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.