0

I am integrating design in Magento2 and want to remove fields from shipping calculate form like I don't want the country and state. Shipping calculates form should contain only one field i.e zipcode .customer can get shipping rate by entering their postcode. How can I do this? We have applied all possible ways to remove country and state field. I also tried it by apply javascript for hiding country field and state field but that's also not working because of the knockout loads' template after load whole page.

3

Hello you can override the block Magento\Checkout\Block\Cart\LayoutProcessor

VendorName/ModuleName/etc/di.xml

Please follow below process to hide country and state from estimate shipping form.

    <?xml version="1.0"?>
<!--
    /**
     * Webkul Grid DI
     *
     * @category    Webkul
     * @package     Webkul_Grid
     * @author      Webkul Software Private Limited
     *
     */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     </type>
        <preference for="Magento\Checkout\Block\Cart\LayoutProcessor" type="VendorName\ModuleName\Block\Cart\LayoutProcessor" />
</config>

Create VendorName\ModuleName\Block\Cart\LayoutProcessor.php

 <?php
/**
 * Hello Rewrite Product ListProduct Block
 *
 * @category    Magecoder
 * @package     Magecoder_Postnetship
 * @author      Magecoder Software Private Limited
 *
 */
namespace Magecoder\Postnetship\Block\Cart;

class LayoutProcessor extends \Magento\Checkout\Block\Cart\LayoutProcessor
{
/**
 * Process js Layout of block
 *
 * @param array $jsLayout
 * @return array
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function process($jsLayout)
{
    $elements = [
        'city' => [
            'visible' => $this->isCityActive(),
            'formElement' => 'input',
            'label' => __('City'),
            'value' =>  null
        ],
        'country_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('Country'),
            'options' => $this->countryCollection->loadByStore()->toOptionArray(),
            'value' => null
        ],
        'region_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('State/Province'),
            'options' => $this->regionCollection->load()->toOptionArray(),
            'value' => null
        ],
        'postcode' => [
            'visible' => true,
            'formElement' => 'input',
            'label' => __('Zip/Postal Code'),
            'value' => null
        ]
    ];

    if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
        ['address-fieldsets']['children'])
    ) {
        $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
        ['children']['address-fieldsets']['children'];
        $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);
        $fieldSetPointer['region_id']['config']['skipValidation'] = true;
    }
    return $jsLayout;
}

} ?>

From the above elements array you can hide the state and country. To hide it just comment state and country from elements array like this:

 public function process($jsLayout)
{
    $elements = [
        'city' => [
            'visible' => $this->isCityActive(),
            'formElement' => 'input',
            'label' => __('City'),
            'value' =>  null
        ],
       /* 'country_id' => [ // Here I am commenting Country and state 
            'visible' => true,
            'formElement' => 'select',
            'label' => __('Country'),
            'options' => $this->countryCollection->loadByStore()->toOptionArray(),
            'value' => null
        ],
        'region_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('State/Province'),
            'options' => $this->regionCollection->load()->toOptionArray(),
            'value' => null
        ],*/
        'postcode' => [
            'visible' => true,
            'formElement' => 'input',
            'label' => __('Zip/Postal Code'),
            'value' => null
        ]
    ];

    if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
        ['address-fieldsets']['children'])
    ) {
        $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
        ['children']['address-fieldsets']['children'];
        $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);
        $fieldSetPointer['region_id']['config']['skipValidation'] = true;
    }
    return $jsLayout;
}
  • 1
    This return region.js:38 Uncaught TypeError: Cannot read property 'is_zipcode_optional' of undefined error – Priya Apr 10 '18 at 11:11

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