views:

33

answers:

1

Hi there, I'm writing an import module to import configurable products into magento, which works quite fine. I've tweaked the import so that it can create all necessary attribute sets, attributes and attribute options needed for creating the configurable products. So far everything works ... quite everything.

When the import creates a new attribute, it can not create the configurable product. When I edit the new attribute in the backend and save it without changes, a message appears which tells me to update some indexes. After I have updated the product flat data index, I can run the import again and everything works fine.

I've tried to ways to create a new attribute:

$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');    
$setup->addAttribute(
                $this->getEntityTypeId(),
                $code,
                array(
                    'attribute_code'            => $code,
                    'label'                     => ucfirst($code),
                    //'group'                   => $attributeSet->getId(),
                    'user_defined'              => 1,
                    'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                    'input'                     => 'select',
                    'unique'                    => 0,
                    'required'                  => 0,
                    'configurable'              => 1,
                    'filterable'                => 1,
                    'visible_on_front'          => 1,
                    'used_in_product_listing'   => 1,
                    'frontend_label'            => array(
                        $code
                    )
                )
            );

The other way is:

$attribute = Mage::getModel("catalog/resource_eav_attribute");
        $attribute->addData(
            array(
                'entity_type_id'            => $this->getEntityTypeId(),
                'attribute_code'            => $code,
                'label'                     => ucfirst($code),
                //'group'                   => $attributeSet->getId(),
                'is_user_defined'           => 1,
                'is_global'                 => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                'frontend_input'            => 'select',
                'is_unique'                 => 0,
                'is_required'               => 0,
                'is_configurable'           => 1,
                'is_filterable'             => 1,
                'is_visible_on_front'       => 1,
                'is_used_in_product_listing'=> 1,
                'frontend_label'            => array(
                    $code
                )
            )
        );

        $attribute->save();

Both codes create the attribute well but I can't use it to create configurable Attributes. I've tried to manually run the index scripts but this does not help me.

What am I doing wrong? Is creating new attributes somehow the black magic of magento? :-D

A: 

I forgot to set the backend type for the new attribute to "int" so it was automatically set to static (which means magento looks for it in the entity table).

The other thing is that i couldn't set the attribute value like this:

$model->setData("newattributecode", 123);

Magento just didn't save the attribute. Instead I had to use the undocumented function Mage_Catalog_Model_Product::addAttributeUpdate() to save the attribute value of this model:

$model->addAttributeUpdate("newattributecode", 123, 0);
Thomas