1

I'm trying to add a custom category attribute to the category collection:

$product = Mage::getModel('catalog/product');
$product->setId($productId);
$categories = $product->getResource()
    ->getCategoryCollection($product)
    ->addAttributeToSelect('some_custom_attribute');

However, some_custom_attribute doesn't get included to the collection data:

[4] => array(11) {
["entity_id"] => string(4) "1164"
["entity_type_id"] => string(1) "3"
["attribute_set_id"] => string(1) "3"
["parent_id"] => string(4) "1151"
["created_at"] => string(19) "2015-04-14 06:19:05"
["updated_at"] => string(19) "2015-04-14 06:19:05"
["path"] => string(13) "1/2/1151/1164"
["position"] => string(1) "3"
["level"] => string(1) "3"
["children_count"] => string(1) "0"
["product_id"] => string(6) "106224"

How can I add the some_custom_attribute to the result?

  • If some_custom_attribute is a new attribute, you need to rebuild the category flat index before it is included in collections – Fabian Schmengler Sep 30 '15 at 15:18
2

try this code (it works for me)

$_categoryCollection = Mage::getResourceModel('catalog/category_collection')
            ->addAttributeToSelect('sales_page_active_from',true)
            ->addAttributeToSelect('sales_page_active_to',true)
            ;
1

This code is not working. So you can use below code as alternatively.

$product = Mage::getModel('catalog/product');
$product->setId($productId);

Get category ids of product

$CategoryIds = $product->getResource()
    ->getCategoryIds($product);

Load category collection

$collection= Mage::getResourceModel('catalog/category_collection');

Add filter category collection by categories ids

$collection->addAttributeToFilter('entity_id',array('in' => implode(',',$CategoryIds)));

Add a custom category attribute to the category collection:

$collection->addAttributeToSelect('some_custom_attribute');

  • Unfortunately, the attribute is not displayed or added. I tried to add a system default attribute, no luck. – user1240207 Sep 30 '15 at 9:02

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.