2

I have added a custom attribute to my category. On category product listing page i want to retrieve the values of that custom attribute. When i got Mage/Catalog/Block/Product/List.php i can find this protected function _getProductCollection() function. Is this function that is getting the collection from the db? If yes how to get my custom attribute values in that method?

Or else which is the method that is responsible for getting collection from db and how to get the attribute value in that method?

EDIT: The purpose is to add some more skus to that collection based on the custom attribute value

  • 1
    Are you talking about a CATEGORY attribute or a PRODUCT attribute? – Fabian Blechschmidt Nov 9 '13 at 8:17
  • if its product attribute and you need to display it in listing page, please confirm you have set that attribute "available in product listing" to yes – Dexter Nov 9 '13 at 10:59
  • @Dexter:its a category attribute. And how to get the current category id from that block method? – zamil Nov 9 '13 at 11:33
3

Product Attribute

Just add it to this xpath:

/config/frontend/product/collection/attributes

You can find an example here:

/app/code/core/Mage/Catalog/etc/config.xml:718

Category Attribute

Just add it to this xpath:

/config/frontend/category/collection/attributes

You can find an example here:

/app/code/core/Mage/Catalog/etc/config.xml:742
  • I am not able to get the current category id itself, I tried with 'echo $category = Mage::getModel('catalog/category')->load($this->getCategoryId());' but it is not echoing any value to the browser – zamil Nov 9 '13 at 8:47
  • And one more thing is i have overridden the function <code>_getProductCollection</code> in my custom module – zamil Nov 9 '13 at 8:50
  • 1
    again, are we talking about category or product attributes? And yes, sure you can't echo a Mage_Catalog_Model_Categry object. IT IS AN OBJECT! :-) You can try to echo $this->getCategoryId() And there is no need to rewrite the _getProductCollection method. – Fabian Blechschmidt Nov 9 '13 at 8:54
  • here i am talking about category attribute. With out re writing _getProductCollection how will i modify my collection? echo $this->getCategoryId(); is not outputting the category id from core method also. – zamil Nov 9 '13 at 9:08
  • getProductCollection gives you products. This has nothing to do with the category. – Fabian Blechschmidt Nov 9 '13 at 9:51
1

You can make an event observer for catalog_block_product_list_collection_prepare.

In config.xml:

<global>
    <events>
        <catalog_block_product_list_collection_prepare>
            <observers>
                <add_custom_products>
                    <type>model</type>
                    <class>my_module/observer</class>
                    <method>productListCollectionAddCustomProducts</method>
                </add_custom_products>
            </observers>
        </catalog_block_product_list_collection_prepare>
    </events>
</global>

In MyModule/Model/Observer.php:

public function productListCollectionAddCustomProducts(Varien_Event_Observer $observer)
{
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
    $collection = $observer->getCollection();
    $filters = $collection->getLimitationFilters();
    if (!isset($filters['category_id']) || empty($filters['category_id']) || !is_string($filters['category_id'])) {
        return;
    }

    $categoryId = $filters['category_id'];
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $myCustomParam = $category->getMyCustomParam();

    if ($myCustomParam == "something") {
        // ...
        // $collection->addItem($product);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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