1

There's an attribute which has been assigend to an attribute set and there's a product using that set.

I have successfully created attributes and added them to attribute sets. However, when I create a new product, those attributes aren't visible in the backend. Also, I can set and retrieve data programmatically, but I cannot see the attributes in the backend.

I have cleared all caches and reindexed everything

EDIT This is what I'm doing at the moment to create the attribute

            /* @var $attributeModel Mage_Catalog_Model_Entity_Attribute */
            $attributeModel = Mage::getModel('catalog/resource_eav_attribute');
            $attributeModel->setEntityTypeId($entityTypeId);
            $attributeModel->setAttributeCode($propName);
            $attributeModel->setBackendType('varchar');
            $attributeModel->setName($propName);
            $attributeModel->setFrontendLabel($propDesc);

            $attributeModel->save();
4
  • Did you set the source model to database? Or created the attribute definition in XML?
    – user4351
    Commented Mar 24, 2015 at 18:26
  • I used the attribute model and created the attributed programmatically. Will post the code later (5 hours or so)
    – Yorrd
    Commented Mar 24, 2015 at 19:49
  • It was probably just incorrectly created. Let's see the code!
    – laketuna
    Commented Mar 25, 2015 at 1:55
  • @musicliftsme there you go, see my edit please
    – Yorrd
    Commented Mar 25, 2015 at 14:47

2 Answers 2

1

It looks to be missing the frontend_input value. Look up the table eav_attribute and see which fields you're missing. One sure way to know which values are required for an attribute you'd like to create programmatically is to create one from the admin and examine the record in eav_attribute.

$attributeModel = Mage::getModel('catalog/resource_eav_attribute')
    ->setEntityTypeId($entityTypeId)
    ->setAttributeCode($propName)
    ->setBackendType('varchar')
    ->setName($propName)
    ->setFrontendLabel($propDesc)
    ->setFrontendInput('multiselect')   // Or could be 'text', 'select', etc.
    ->setBackendModel(...) // not necessarily required 
    ->save();
2
  • Thank you, this was the solution. I have no idea what made me ommit this because I actually had it earlier and thought it was unnecessary
    – Yorrd
    Commented Mar 25, 2015 at 15:13
  • This still isn't correct. You won't find your attribute via Eav/Attribute collection, which is used in a few places. I've bookmarked your question to remind me when I'm behind something else than a smartphone ;-)
    – user4351
    Commented Mar 25, 2015 at 19:03
1

When you create an attribute programmatically, the default source model is Xml. Which means a definition of the attribute is supposed to exist in your module config.

You have to set the source model to table (don't have the exact string available at the moment, but look into Mage/Eav source models), so that the attribute definition is looked up from there.

EDIT:

As posted above, you're missing the frontendInput, but also the source model:

/* @var $attributeModel Mage_Catalog_Model_Entity_Attribute */
$attributeModel = Mage::getModel('catalog/resource_eav_attribute');
$attributeModel->setEntityTypeId($entityTypeId);
$attributeModel->setAttributeCode($propName);
$attributeModel->setBackendType('varchar');
$attributeModel->setName($propName);
$attributeModel->setFrontendLabel($propDesc);
$attributeModel->setFrontendInput('select');
$attributeModel->setSourceModel('eav/entity_attribute_source_table');

$attributeModel->save();

Now, I'm being a bit mean :) but it's for educational purposes!

The method Mage_Eav_Model_Entity_Attribute_Abstract::usesSource defines when a source model is needed:

    return $this->getFrontendInput() === 'select' || $this->getFrontendInput() === 'multiselect'
        || $this->getData('source_model') != '';

So, the answer provided above, you really need a source model! This is because selects and multi-selects are compound fields, so the source model needs to take care of fetching the selected value of the compound to a single entity to be referenced in the attribute field. This also means, that in the "table" source model, that is used and prepared for you in this case, you cannot use a varchar attribute. The backend model needs to be an integer as it will reference the autoincrement id of the ManyToMany relation table eav_attribute_option_value.

Hope this helps you understand things better.

1
  • please check out my edit, I believe I am already doing what you're asking me to
    – Yorrd
    Commented Mar 25, 2015 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.