Magento – Magento 1.8.1 – Layered Navigation not showing attributes created programmatically

layered-navigationmagento-1.8

I have 4 products in 1 category.
Each of them has a attribute called 'material'. The values are 'ZK' for 3 of them and 'FE' for the other.

The 'material' attribute is defined as 'Filterable (with results)'.

The layered navigation doesn't show even after clearing cache and reindexing.

I've seen that at Mage_Catalog_Block_Layer_View

public function canShowOptions()
{

    foreach ($this->getFilters() as $filter) {
        if ($filter->getItemsCount()) {
            return true;
        }
    }

    return false;
}

$filters are correct, but getItemsCount() always returns ZERO. In fact, changing attribute to 'Filterable (without results)' does show the attribute in layered navigation.

UPDATE

Seems to fail only for attributes set programmatically, because I just created one manually and works perfectly after assignment to products. Any idea what could have possibly gone wrong creating and assigning those attributes (and products) programmatically taking into consideration everything goes fine but layered navigation?

This situation is the same with 3 other custom attributes affecting these and other products.

Values are set like this:

$product->setData($attr, $setValue);

And product tab shows correct values, but table catalog_product_entity_int doesn't hold any value for those attributes. Instead they go to catalog_product_entity_varchar. I don't know whether this is important or not, but catalog_product_index_eav does not include values for my attributes.

UPDATE
Mage_Catalog_Model_Resource_Eav_Attribute

public function isIndexable()
{

    if ($backendType == 'int' && $frontendInput == 'select') {
        return true;
    } else if ($backendType == 'varchar' && $frontendInput == 'multiselect') {
        return true;
    } else if ($backendType == 'decimal') {
        return true;
    }

    return false;
}

So now I know… being considered a varchar backend-type attribute IS actually the problem. But WHY is it so?

I don't know where else to look. Please help. Will post/send code if needed to solve this issue, only that is very big to put it here.

SOLUTION

Obvious once I realized what the problem was. Like I said attribute must be $backendType == 'int' for select, $backendType == 'varchar' for multi-select or 'decimal'.

Best Answer

As I said, solution was quite easy once I realized what the problem was.

I followed this Magento Wiki to setup attributeSets and attributes, only I didn't realize my attributes had to be of 'int' backend-type to be eligible for layered navigation.

So, in order to create an attribute for layered navigation you must have into consideration:

  • Frontend-type must be price, select or multiselect

  • Backend-type must be decimal (like price), int (for selects), varchar (for multiselect). There's no need to worry about this unless you are, like me, creating attributes programmatically.

  • That it must be marked as 'use in Layered Navigation'

  • That there should be at least two products with different values for this attribute (this seems to be not mandatory, but gives sense to use the attribute in Layered Navigation)

Related Topic