7

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.

7

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.

  • 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 '16 at 14:44
  • Alan Storm has a great series of magento 2 tutorials on his site, that would be a good place to start. – Aaron Allen Sep 26 '16 at 5:22
  • @AaronAllen, How to remove State/Province from Estimate shipping tax section of cart page? – jafar pinjar Nov 26 '18 at 15:20
  • it worked for checkout .but in my account states no change – Sairaj Gadekar Jan 9 '19 at 9:39
4

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

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