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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou 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>
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.
I ended up just purchasing this small extension which allows full management of regions through the Magento 2 admin interface: