Adding attributes in magento from extension - solved
-
12-12-2019 - |
Question
i am trying to insert some attributes (drop-down) into the magento. The creation of the attribute with option is completed successfully. I also create an attribute set based on that attribute successfully. The problem is that when i add a product based on that attribute set the product is not visible on the website.
Here is my configuration for the dropdown attribute:
$data = array(
'is_global' => '1',
'frontend_input' => 'select',
'default_value_text' => '',
'default_value_yesno' => '0',
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => '0',
'is_required' => '1',
'frontend_class' => '',
'is_searchable' => '0',
'is_visible_in_advanced_search' => '0',
'is_comparable' => '0',
'is_used_for_promo_rules' => '0',
'is_html_allowed_on_front' => '1',
'is_visible_on_front' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'is_configurable' => '1',
'is_filterable' => '0',
'is_filterable_in_search' => '0',
'backend_type' => 'varchar',
'default_value' => '',
'option' => ''
);
--
i 've changed the above array to this one:
$data = array(
'is_global' => true,
'frontend_input' => 'select',
'is_required' => true,
'visible' => true,
'is_html_allowed_on_front' => true,
'is_visible_on_front' => true,
'is_configurable' => true,
'is_filterable' => true,
'backend_type' => 'varchar',
'apply_to' => array('simple'),
'default_value' => '',
'position' => 1,
'visible_on_front' => true,
'option' => ''
);
but i still have the same problem. i think that i am missing something here.
Solution 3
the correct array with the attributes is the following:
$data = array(
'is_global' => true,
'frontend_input' => 'select',
'is_required' => true,
'visible' => true,
'is_html_allowed_on_front' => true,
'is_visible_on_front' => true,
'is_configurable' => true,
'configurable' => true,
'is_configurableSpecified' => true,
'is_visible_on_frontSpecified ' => true,
'is_filterable' => true,
'apply_to' => 'simple',
'default_value' => '',
'position' => 1,
'visible_on_front' => true,
'option' => '',
'backend_type' => 'int'
);
it seems that the 'backend_type' attribute should be set to 'int' to setup the attribute as it should be.
thanks
OTHER TIPS
Here is what I used recently in my project and successfully created it programatically. I see that your apply_to
is using array, though I think you don't need array and can pass multiple product types comma-separated.
$model = Mage::getResourceModel('catalog/setup','catalog_setup');
$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);
$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);
$model->addAttributeToSet(
'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group
Update - To show on frontend use visible_on_front
If you take a look at /app/code/core/Mage/Catalog/Model/Resource/Setup.php
/**
* Add attribute to an entity type
*
* If attribute is system will add to all existing attribute sets
*
* @param string|integer $entityTypeId
* @param string $code
* @param array $attr
* @return Mage_Eav_Model_Entity_Setup
*/
public function addAttribute($entityTypeId, $code, array $attr)
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$data = array_merge(
array(
'entity_type_id' => $entityTypeId,
'attribute_code' => $code
),
$this->_prepareValues($attr)
);
$this->_prepareValues($attr)
will set is_visible_on_front
from visible_on_front
/**
* Prepare catalog attribute values to save
*
* @param array $attr
* @return array
*/
protected function _prepareValues($attr)
{
$data = parent::_prepareValues($attr);
$data = array_merge($data, array(
'frontend_input_renderer' => $this->_getValue($attr, 'input_renderer'),
'is_global' => $this->_getValue(
$attr,
'global',
Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
),
'is_visible' => $this->_getValue($attr, 'visible', 1),
'is_searchable' => $this->_getValue($attr, 'searchable', 0),
'is_filterable' => $this->_getValue($attr, 'filterable', 0),
'is_comparable' => $this->_getValue($attr, 'comparable', 0),
---> 'is_visible_on_front' => $this->_getValue($attr, 'visible_on_front', 0),
'is_wysiwyg_enabled' => $this->_getValue($attr, 'wysiwyg_enabled', 0),
'is_html_allowed_on_front' => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
'is_filterable_in_search' => $this->_getValue($attr, 'filterable_in_search', 0),
'used_in_product_listing' => $this->_getValue($attr, 'used_in_product_listing', 0),
'used_for_sort_by' => $this->_getValue($attr, 'used_for_sort_by', 0),
'apply_to' => $this->_getValue($attr, 'apply_to'),
'position' => $this->_getValue($attr, 'position', 0),
'is_configurable' => $this->_getValue($attr, 'is_configurable', 1),
'is_used_for_promo_rules' => $this->_getValue($attr, 'used_for_promo_rules', 0)
));
return $data;
}
Try adding to your array option
array(
....
'visible_on_front' => 1,
....
)
You can either :
Remove and re-add using
// Remove Product Attribute
$installer->removeAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code');
Or Update
$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code', array(
'visible_on_front' => 1,
));
(assuming that your installer is of type Mage_Catalog_Model_Resource_Setup)
<?xml version="1.0"?>
<config>
.....
<models>
<attr>
<class>MagePal_Attr_Model</class>
<resourceModel>attr_mysql4</resourceModel>
</attr>
</models>
<resources>
<productattr_setup>
<setup>
<module>MagePal_Attr</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
.......
</resources>
</global>
</config>
If not, then in your mysql4-install-*.php file add
<?php
$installer = $this;
$installer->startSetup();
$setup = new Mage_Catalog_Model_Resource_Setup('core_setup');
//$setup->removeAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code');
$setup->addAttribute(...);