3

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.

4

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 :)

1

If your attribute in some cases can be NULL, method addAttributeToSelect could not work. Take a look at classes providing methods for collection. I don't remember it now well, but it was something like joinAttribute method and it was possible aparat of code of attribute add second argument to function and it was type of join. So if you want to grab attribute values (if there are cases when it can be empty) run something like joinAttibute('from_price', 'left')

  • Thanks. manually joining the attribute should work as i could just extend the select query. But by manually joining i have a lot of code to review and it still doesn't fix the problem. However i could use this as a temporary fix so thanks for suggesting it :). The strange thing remains as soon as i get my category from the collection my attribute is completely missing where as the attributes "latijnse_naam" and "extra_description" are there with no extra code. – Jerry Jun 20 '16 at 7:47
0

In the module XML try adding:

<config>
    <frontend>
        <category>
            <collection>
                <attributes>
                    <from_price/>
                </attributes>
            </collection>
        </category>
    </frontend>
</config>

However looking at your module's setup, you don't need the source model defined as it's a text field.

  • this will add attribute to flat category I think - agreed - source model is not useful at all here. – Bartosz Kubicki Jun 17 '16 at 19:14
  • Thanks for the suggestion i tried it but unfortunately it did not fix the problem. – Jerry Jun 20 '16 at 7:36

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.