8

How can I remove Guam, Puerto Rico, Palau, and all other US Territories from the "State" (or "Region") dropdown list on both the cart page and checkout pages?

I only want to ship to the 48 contiguous states.

0

3 Answers 3

8

You can filter out the options using a Plugin for the toOptionArray() method of Magento\Directory\Model\ResourceModel\Region\Collection:

class StateFilter
{
    protected $disallowed = [
        'Guam',
        'Puerto Rico',
        'Palau',
        'Virgin Islands',
        'Northern Mariana Islands',
        'Marshall Islands',
        'Federated States Of Micronesia',
        'American Samoa',
        'Armed Forces Africa',
        'Armed Forces Americas',
        'Armed Forces Canada',
        'Armed Forces Europe',
        'Armed Forces Middle East',
        'Armed Forces Pacific',
        'Hawaii',
        'Alaska'
    ];

    public function afterToOptionArray($subject, $options)
    {
        $result = array_filter($options, function ($option) {
            if (isset($option['label']))
                return !in_array($option['label'], $this->disallowed);
            return true;
        });

        return $result;
    }
}

To get the Plugin working, add this to your module's frontend di.xml:

<type name="Magento\Directory\Model\ResourceModel\Region\Collection">
    <plugin name="MyCompany_MyModule_Limit_State_Filter_Plugin" type="MyCompany\MyModule\Plugin\StateFilter"/>
</type>

More information about creating Plugins in Magento 2 here.

5
  • Thanks. I'm just learning Magento and development. Any links to point me in the right direction on how to use a plugin like you mentioned?
    – Tim K.
    Sep 25, 2016 at 14:44
  • Alan Storm has a great series of magento 2 tutorials on his site, that would be a good place to start. Sep 26, 2016 at 5:22
  • @AaronAllen, How to remove State/Province from Estimate shipping tax section of cart page? Nov 26, 2018 at 15:20
  • it worked for checkout .but in my account states no change Jan 9, 2019 at 9:39
  • Hello @AaronAllen, Is there any way to display this region only for shipping address? like can we bifurcate address type in this code? Apr 21, 2021 at 15:13
5

If you want to remove the states during checkout and also while adding or editing address from my account then you need the following solution

Step 1 app/code/NAMESPACE/CustomDirectory/etc/di.xml

<type name="Magento\Directory\Model\ResourceModel\Region\Collection">
   <plugin name="Namespace_CustomDirectory::region_collection" type="Namespace\CustomDirectory\Plugin\Directory\Model\ResourceModel\Region\StateFilter" sortOrder="1" disabled="false"/>
</type>
<!-- We can not write a plugin for Magento\Directory\Helper\Data so we have to rewrite it. And it is for address from my account -->
<preference for="Magento\Directory\Helper\Data" type="Namespace\CustomDirectory\Rewrite\Directory\Helper\Data" />

Step 2 (Regions drop-down during Checkout):

namespace Namespace\CustomDirectory\Plugin\Directory\Model\ResourceModel\Region;


class StateFilter
{
   protected $disallowed = [
    'Guam',
    'Puerto Rico',
    'Palau',
    'Virgin Islands',
    'Northern Mariana Islands',
    'Marshall Islands',
    'Federated States Of Micronesia',
    'American Samoa',
    'Armed Forces Africa',
    'Armed Forces Americas',
    'Armed Forces Canada',
    'Armed Forces Europe',
    'Armed Forces Middle East',
    'Armed Forces Pacific',
    'Hawaii',
    'Alaska'
];

    public function afterToOptionArray(\Magento\Directory\Model\ResourceModel\Region\Collection $subject, $options)
    {
        $result = array_filter($options, function ($option){
            if(isset($option['label']))
                return !in_array($option['label'], $this->disallowed);
            return true;
        });

        return $result;
    }
}

Step 3 (For Address Book):

namespace Namespace\CustomDirectory\Rewrite\Directory\Helper;


class Data extends \Magento\Directory\Helper\Data
{
   protected $disallowed = [
        'Guam',
        'Puerto Rico',
        'Palau',
        'Virgin Islands',
        'Northern Mariana Islands',
        'Marshall Islands',
        'Federated States Of Micronesia',
        'American Samoa',
        'Armed Forces Africa',
        'Armed Forces Americas',
        'Armed Forces Canada',
        'Armed Forces Europe',
        'Armed Forces Middle East',
        'Armed Forces Pacific',
        'Hawaii',
        'Alaska'
    ];

    /**
     * Retrieve regions data json
     *
     * @return string
     */
    public function getRegionJson()
    {
        \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
        if (!$this->_regionJson) {
            $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
            $json = $this->_configCacheType->load($cacheKey);
            if (empty($json)) {
                $regions = $this->getRegionData();
                if(isset($regions['US'])) {
                    $regions['US'] = array_filter($regions['US'], function ($region) {
                        if (isset($region['name']))
                            return !in_array($region['name'], $this->disallowed);
                        return true;
                    });
                }
                $json = $this->jsonHelper->jsonEncode($regions);
                if ($json === false) {
                    $json = 'false';
                }
                $this->_configCacheType->save($json, $cacheKey);
            }
            $this->_regionJson = $json;
        }

        \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
        return $this->_regionJson;
    }
} 

I hope it will help you.

0

I ended up just purchasing this small extension which allows full management of regions through the Magento 2 admin interface:

https://store.eltrino.com/region-manager-for-magento-2.html

1
  • @Abbas Thanks !! Apr 7, 2020 at 8:17

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