Magento Layered Navigation – Programmatically Created Attributes

attribute-setattributeslayered-navigationproduct-attribute

I'm creating an attribute set then creating several attribute groups and add them into the previously created attribute set, then createing several attributes and fill them up with options.
It goes well and works fine.

After all this i set some products attribute set to use my newly created set and update the fields for the product.
This one goes well too.

All this is done programatically.(There is no way to create thousands of attributes,sets,groups and update 22000 products also all of this data comes from xml)

The problem is that, i cannot filter these attributes on layered nivagation. unless i go to each attribute change anything on them manually, then save. Then i go to each product change each attribute manually then save.

The question is that would i miss something in my code so it won't pick up until i change all this manually too?

So again:
The attributes are there. The product filled up.
The attributes won't show up on layered navigation until i edit anything on them manually and i edit the corresponding fields for every product.

Here are some code(there are more than these, but these snippets are the main parts where something could go wrong):

//attribute set creation
$this->setapi=Mage::getModel('catalog/product_attribute_set_api');
$this->setapi->create($newSetName, 4);

//attribute group creation
Mage::getModel('eav/entity_attribute_group')
            ->setId(null)
            ->setAttributeSetId($attributeSetId)
            ->setAttributeGroupName($attributegroupname)
            ->save();

//attribute creation
$this->installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$this->installer->startSetup();
$this->installer->addAttribute(
            Mage_Catalog_Model_Product::ENTITY,
            $this->convertAttributeCode($attributename),
            array(
                'label'                      => $attributename,
                'type'                       => 'text',
                'input'                      => 'select',
                'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                'user_defined'               => true,
                'required'                   => false,
                'visible'                    => true,
                //'source'                     => 'eav/entity_attribute_source_table',
                'backend'                    => '',
                'frontend'                   => '',
                'searchable'                 => true,
                'visible_in_advanced_search' => true,
                'filterable_in_search'       => true,
                'is_html_allowed_on_front'   => true,
                'sort_order'                 => '50',
                'filterable'                 => true,
                'comparable'                 => true,
                'visible_on_front'           => false, // id ont need them on prduct page. just in layered navigation and search
                'is_configurable'            => false
            )
        );
//add attribute to set and group
$this->setapi->attributeAdd($codeid,$attributesetid,$attributegroupid);
//add option
$result = array('attribute_id' => 
        Mage::getModel('eav/entity_attribute')->getIdByCode(
             Mage_Catalog_Model_Product::ENTITY, 
             $attributename
        )
     );
    $result['values'][0] = $option;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->addAttributeOption($result);

//set product's attribute set then fill up with the attributes
$this->attributeClass->endSetup();
    $product=Mage::getModel("catalog/product")->load($id);
    $product->setAttributeSetId($setid)->save();
    print_r($attributevalues);
    foreach($attributevalues as $code=>$value){
            $product->setData($code,$value); //attribute code and option id
    }
    $product->save();

Thank you for you help.

Update

'type' => 'int',

Is the solution

Best Answer

Problem here is that you want to use a text type attribute for magento layered navigation.

Magento only supports the int type as valid layered navigation attribute. The types select and multi select use this attribute type.

So if you programmatically add the attribute use type => 'int' and if you manually add the attribute make sure you set the correct catalog input type for store owner: At the bottom of this image

Related Topic