Magento 1.8 – Fix Missing Custom Attributes in Category Collection

categorycategory-attributecollection;magento-1.8

I am currently developing for a shop that doesn't use the product page. On every category with sub categories the sub categories are shown until there are no more sub categories, then the products in that category are shown. Downside of this workflow is the really large category tree.

To speed things up i added all calculated from prices to the category using a custom attribute "from_price" and i'm trying to replace the category load with the category collection.

When i preform a load on the category the data my attribute is there.

example:

$category = Mage::getModel('catalog/category')->load(5); 
var_dump($category->getData());

array (size=45)
  'entity_id' => string '6' (length=1)
  'parent_id' => string '5' (length=1)
  'created_at' => string '2012-08-02 08:28:34' (length=19)
  'updated_at' => string '2016-06-14 13:37:09' (length=19)
  'path' => string '1/2/5/6' (length=7)
  'position' => string '2' (length=1)
  'level' => string '3' (length=1)
  'children_count' => string '3' (length=1)
  'store_id' => string '3' (length=1)
  'extra_description' => string '' (length=0)
  'latijnse_naam' => string '' (length=0)
  'from_price' => string '4.09' (length=4)
  ...

But when i try to get the attribute using a collection my attribute is not added to the data.

example:

$collection = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*');

foreach ($collection as $category) {
    var_dump($category->getData());
}

array (size=36)
  'entity_id' => string '6' (length=1)
  'parent_id' => string '5' (length=1)
  'created_at' => string '2012-08-02 08:28:34' (length=19)
  'updated_at' => string '2016-06-14 13:37:09' (length=19)
  'path' => string '1/2/5/6' (length=7)
  'position' => string '2' (length=1)
  'level' => string '3' (length=1)
  'children_count' => string '3' (length=1)
  'store_id' => string '3' (length=1)
  'extra_description' => string '' (length=0)
  'latijnse_naam' => string '' (length=0)
  ...

The strange thing is the attributes "extra_description and "latijnse_naam" are added the same way as my "from_price" attribute and they both show up in the data collections.

My code to add the attribute looks like this ( upgrade-2.0.0-2.0.1.php )

$installer = $this;
$installer->startSetup();

$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'from_price', array(
    'type'              => 'varchar',
    'label'             => 'From Price',
    'input'             => 'text',
    'source'            => 'eav/entity_attribute_source_boolean',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '0',
));
$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'from_price',
    '25'
);

$installer->endSetup();

Things i have tried so far:

  • Removed and readded the attribute
  • comparing the inserted data in the eav_attribute,
    catalog_eav_attribute, eav_entity_attribute with the data of the 2
    attributes that do show up
  • Upgraded ( local ) magento from 1.8.1.0 to 1.9.2.4

Final thing i tried was turning on the flat category tables. When i have this option turned on i do get my attribute but i do not want to be depending on flat category tables and so i would like to fix this issue :).

Has any one else encountered this issue with magento or are there any suggestions in which direction i should look.

All answers are appreciated.

Best Answer

Its been a while since i have asked this and by now i know the answer of my own question.

The answer is that Magento requires a default value of an attribute in the current collection to add the attribute to your collection object.

In my case i only filled in values at storeview level which caused Magento not to see my attribute in its collection.

For those who are interested in how Magento does the above, look at :

app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php On line 1109 ( magento 1.9.2.4 )

Thanks for your suggestions, hope this will help someone :)