Magento 2 – Removing Various States/Provinces from Shipping but Not Billing Address

billing-addressmagento2shipping-addressstate

I've searched all around and haven't been able to find a solution to this.

I'm on Magento 2.1.7 and need to remove the option for shipping to any province other than Ontario (Canada) from my check-out page. But at the same time I need billing addresses from other provinces to be accepted, just no shipments to anywhere other than Ontario.

Thanks in advance for the help!

Best Answer

It is possible to change it in the checkout for example create these module files

Block/Checkout/LayoutProcessor.php

<?php
namespace Vendor\Module\Block\Checkout;

use Magento\Directory\Helper\Data as DirectoryHelper;

/**
 * Class LayoutProcessor
 * @package Vendor\Module\Block\Checkout
 */
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
{


    /**
     * @var DirectoryHelper
     */
    protected $directoryHelper;

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

    /**
     * @param array $result
     * @return array
     */
    public function process($result)
    {

        if ($result['components']['checkout']['children']['steps']
        ['children']['shipping-step']['children']['shippingAddress']) {



                    $shippingAddressFieldSet = $result['components']['checkout']['children']['steps']
                    ['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];

                    $regionOptions = $shippingAddressFieldSet['region_id']['options'];

                    // update the options array according to your logic $regionOptions

                    $shippingAddressFieldSet['region_id']['options'] = $regionOptions;

                    $result['components']['checkout']['children']['steps']
                    ['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'] = $shippingAddressFieldSet;
                }
            }
        }


        return $result;
    }
}

and then your frontend di

etc/frontend/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\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="vendor_module_layoutprocessor" xsi:type="object">Vendor\Module\Block\Checkout\LayoutProcessor</item>
            </argument>
        </arguments>
    </type>
</config>
Related Topic