1

I am making a simple plugin that creates few example products/categories and attributes for the customers to see as an example when they start they online shop using Magento. It creates everything but has some problems with attributes.

The problem is, that attribute is created, it is added to the default set and is shown in the products and Magento admin system. But when I try to save the product, the attribute value is not saved unless I first go to the attribute and re-save it manually.

Here is my attribute creation code:

private function createAttribute()
{       
    $attribute = Mage::getModel('catalog/resource_eav_attribute');
    $_attribute_data = array(
        'attribute_code' => 'demo_size',
        'is_global' => '1',
        'frontend_input' => 'select', //'boolean',
        'default_value_text' => '',
        'default_value_yesno' => '0',
        'default_value_date' => '',
        'default_value_textarea' => '',
        'is_unique' => '0',
        'is_required' => '1',
        'apply_to' => '0', //array('grouped')
        'is_configurable' => '1',
        'is_searchable' => '0',
        'is_visible_in_advanced_search' => '1',
        'is_comparable' => '1',
        'is_used_for_price_rules' => '0',
        'is_wysiwyg_enabled' => '0',
        'is_html_allowed_on_front' => '1',
        'is_visible_on_front' => '0',
        'used_in_product_listing' => '0',
        'used_for_sort_by' => '0',
        'frontend_label' => 'Demo Size',
        'option' => array(
            'value' => array(
                'optionone' => array('XXL'),
                'optiontwo' => array('L'),
                'optionthree' => array('S'),
            ),
        ),
        'default' => array(
            'optionthree'
        )
    );

    $attribute->addData($_attribute_data);
    $attribute->setAttributeSetId(4);
    $attribute->setAttributeGroupId(7);
    $attribute->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
    $attribute->setIsUserDefined(1);


    try {
        $attribute->save();
        $this->attId = $attribute->getId();
    } catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }  
}

So am I missing something when the attribute is created, or is there anything else I have to do, so that the attribute would work properly without me having to re-save it manually?

1

When you have created the attribute did you reindex usually it causes a message saying product attributes need to be reindexed so if you go to the System -> Index Management you will likely see one or more indexers saying that it requires to be Reindexed.

Either run the indexer there or trigger it from your shell and then retry you should find it will start working.

Also consider clearing your cache in var/cache as this is another common reason for newly created fields etc not being saved even after they show up as if they are working properly.

Update:

Just had an additional look over what you have there and I think you might ened to define the type of the attribute you are creating.

$_attribute_data['type'] = 'varchar';
$_attribute_data['backend'] = 'eav/entity_attribute_backend_array'; 

I have set those onto a multiselect which I was using - by default it was using an int type to store it - your values would not fit into that since your using textual sizes and the format it is coming back was actually an array.

You should use the removeAttribute() to take out it and re-run the setup script to initialise this correctly.

  • I run the indexing and clear cache. That only removes the warning message, but it still does not save the attribute value on product. – Mmazvis Mar 2 '15 at 11:22
  • 1
    Based on what you have stated @Mmazvis I have had a look at a similar issue which I recently worked on - the backend type being incorrect for the format which it was attempting to save. Have a look at the updated answer and see if that perhaps helps. – John Cuthbert Mar 2 '15 at 11:39
  • It wasn't working at first, but I just inserted it as 'backend_type' =>'varchar', 'backend_model' => 'eav/entity_attribute_backend_array', into the $_attribute_data and everything started working without any problems. Thanks for help. – Mmazvis Mar 2 '15 at 13:37

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.