Zifius
Zifius

Reputation: 1650

The fastest way to get attribute values on product listing

I'm looking for the most effective way to get configurable product attribute values on product listing pages, for example color values for a product (from assigned simple products)

Currently looking into utilising catalog_product_flat_n table for this purpose, but perhaps there is an easier or more correct approach to do this? I'm trying to avoid using

$product->getTypeInstance()->getConfigurableAttributesAsArray()

on every product, as this will be very slow

Thanks

Upvotes: 2

Views: 188

Answers (1)

Nick
Nick

Reputation: 602

I had the same issue, so I created my own resource model for getting data from flat tables, look into code below

  <?php

    class NameSpace_ModuleName_Model_Resource_Colors extends
        Mage_Core_Model_Resource_Db_Abstract
    {
        protected $_storeId;

        protected function _construct()
        {
            $this->_init('catalog/product_flat', 'entity_id');
            $this->_storeId = (int)Mage::app()->getStore()->getId();
        }

        public function getData($entityId)
        {
            $resource = Mage::getSingleton('core/resource');
            $select = $resource->getConnection('core_read')->select();
            $select
                ->from($this->getTable(array('catalog/product_flat', $this->_storeId)), '*')
                ->where('entity_id = :entity_id');



            $result = $resource->getConnection('core_read')->fetchAll($select, array('entity_id' => $entityId));

            return $result;
        }
    }

Hope it will be helpful for you

Upvotes: 1

Related Questions