I wish to apply a filter on the states being loaded anywhere on the website. So far I have got to know that the toOptionArray() function of Magento\Directory\Model\ResourceModel\Region\Collection class has this data. So i need to extend this function in my child class. How to override it ?

I have tried rewriting through di.xml of my custom module:

<preference for="Magento\Directory\Model\ResourceModel\Region\Collection" type="CustomNameSpace\CustomModule\Model\ResourceModel\Region\Collection" />

and extended this customclass from the actual Collection class. But the code doesn't pass through my custom toOptionArray() function and continues to pass through the base class' function

1 Answer 1


You can use a plugin for the collection:


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Filter inactive regions plugin -->
    <type name="Magento\Directory\Model\ResourceModel\Region\Collection">
        <plugin name="plugin_filter_inactive_regions"


namespace Vendor\Module\Model\Plugin;

use Magento\Directory\Model\ResourceModel\Region\Collection as OriginalRegionsCollection;

class RegionCollection
     * Filter regions collection by active regions
     * @param OriginalRegionsCollection $collection
     * @param bool|false $printQuery
     * @param bool|false $logQuery
     * @return array
    public function beforeLoad(
        OriginalRegionsCollection $collection,
        $printQuery = false,
        $logQuery = false
    ) {
        if (!$collection->isLoaded()) {
            $collection->addCountryFilter('US'); // show US only regions

        return [$printQuery, $logQuery];

As the method toOptionArray uses a collection, all should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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