1

I have written the following code to create an attribute in magento 1.9

$name = $data['name'];
$attributeSetId = $data['attributeSetId'];
$attributeGroupId = $data['attributeGroupId'];

//create a new attribute
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$attribute = array(
  'type'              => 'varchar',
  'backend'           => '',
  'frontend'          => '',
  'label'             => $data['title'],
  'input'             => $data['type'],
  'class'             => '',
  'source'            => 'catalog/product_attribute_source_layout',
  'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
  'visible'           => true,
  'required'          => false,
  'user_defined'      => true,
  'default'           => '',
  'searchable'        => false,
  'filterable'        => false,
  'comparable'        => false,
  'visible_on_front'  => false,
  'unique'            => false
);

if(array_key_exists('options', $data) && !empty($data['options']) && $data['type'] == 'select') {
  Mage::log('adding select options to attribute');

  $values = array();

  foreach ($data['options'] as $option) {
    $values[$option['value']] = $option['label'];
  }

  $options = array(
    'values' => $values
  );

  $attribute['option'] = $options;

}

if(array_key_exists('isGlobal', $data) && !empty($data['isGlobal']) && $data['isGlobal']) {
  $attribute['global'] = 1;
}

if(array_key_exists('isConfigurable', $data) && !empty($data['isConfigurable']) && $data['isConfigurable']) {
  $attribute['is_configurable'] = 1;
}


$entityTypeId = $setup->getEntityTypeId('catalog_product');
$setup->addAttribute($entityTypeId, $name, $attribute);

Which is creating the attribute correctly in Magento, along with the attribute options. Which I can see are correct when I edit the attribute in the admin site.

However when I go to use the attribute on a product the attributes are incorrect.

Instead of being my options, for example (small,medium,large) I am seeing the following values in the dropdown (No layout updates, Empty, 1 column, 2 columns with left bar, 2 columns with right bar, 3 columns). Which are being pulled from the "Page Layout" attribute.

Elsewhere in my code I am pulling the attribute options out from the attribute, when I use the following code I get my correct options:

$options = Mage::getResourceModel('eav/entity_attribute_option_collection');
          $attributeOptions  = $options->setAttributeFilter($attribute->getId())
                                       ->setStoreFilter(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)
                                       ->toOptionArray();

When I use the following code, it pulls out the incorrect attribute options:

$attributeOptions = $attribute->getSource()->getAllOptions(false);
3

This is your problem

'source'            => 'catalog/product_attribute_source_layout',

It should not be there.
remove the attribute from your db and reinstall your module.
Or simply edit the attribute from the eav_attribute table and remove the value of the column source_model.

Let's take a look at the method getSource that you mentioned.

public function getSource()
{
    if (empty($this->_source)) {
        if (!$this->getSourceModel()) {
            $this->setSourceModel($this->_getDefaultSourceModel());
        }
        $source = Mage::getModel($this->getSourceModel());
        if (!$source) {
            throw Mage::exception('Mage_Eav',
                Mage::helper('eav')->__('Source model "%s" not found for attribute "%s"',
                    $this->getSourceModel(),
                    $this->getAttributeCode()
                )
            );
        }
        $this->_source = $source->setAttribute($this);
    }
    return $this->_source;
}

this means that if you specify a source_model tot the attribute, that class will be instantiated and returned by the getSource method. If not, then the default source model (which is defined in the eav_entity_type table for each entity) will be used.
But in your case catalog/product_attribute_source_layout maps to the class Mage_Catalog_Model_Product_Attribute_Source_Layout so the method getAllOptions for that class will be called and you get back the strange results.

  • Working in a sandbox magento at the minute so deleting the attribute will be fine. Will test this out. Thanks for answering – Matthew Wilson Nov 23 '16 at 21:24
  • I've updated the answer with some explanations about why you get this problem. – Marius Nov 23 '16 at 21:36

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.