Magento 2 : How to change state list for USA? [duplicate]
-
25-01-2021 - |
Solution
This issue has already been asked and resolved here - How to change state list for USA in Magento 2?
Quoting it for your reference;
You can filter out the options using a Plugin for the
toOptionArray()
method ofMagento\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][1].
[1]: http://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange