Dane Lowe
19 April 2013

How to get an EAV product collection when Flat Catalog is Enabled

There are certain times when your attribute filters just won't work when you're using a flat catalog product collection, namely when you're filtering by multivalued attributes, or attributes not in the index.

This simple fix will allow you to programmatically choose whether the collection should act as an EAV collection or as the default collection specified by admin.

The description assumes you already have an extension set up (of course you already have one set up if you need this)

Create a Resource Model that overloads Mage_Catalog_Model_Resource_Product_Collection

* Override core Product Collection Model to allow programmatically disabling flat catalog.
class Avid_TTCatalog_Model_Rewrite_Catalog_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
    protected $_disableFlat = false;

    public function isEnabledFlat()
        if ($this->_disableFlat)
        return false;
        return parent::isEnabledFlat();

    public function setDisableFlat($value)
        $this->_disableFlat = (boolean)$value;
        $type = $value ? 'catalog/product' : 'catalog/product_flat';
        return $this;

And of course define the overload in your extension's config.xml

<?xml version="1.0" encoding="UTF-8"?>

Then, you can disable flat catalog on any product collection immediately after instantiating it, e.g.

$collection = Mage::getModel('catalog/product')->getCollection()
comments powered by Disqus