Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active July 11, 2019 23:39
Show Gist options
  • Save 0-Sony/50845f1d8b55f6673650 to your computer and use it in GitHub Desktop.
Save 0-Sony/50845f1d8b55f6673650 to your computer and use it in GitHub Desktop.
Add Custom Attribute for products in custom Attributes set
<!-- local/Namespace/Catalog/data/Namespace_catalog_setup/data-install-0.0.1.php -->
<?php
try {
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;
/**
* Custom attribute creation
*/
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'my_custom_attribute', array(
/** let group empty to avoid adding attribute to all Attribute Sets if you have several Attribute Sets **/
/** otherwise you can set "General" for example **/
'group' => '',
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'My Custom Attribute',
'input' => 'text',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'user_defined' => true, /** Define this property in order to avoid that this attribute is set un general group */
'visible' => true,
'required' => false,
'visible_on_front' => true,
'used_in_product_listing' => false,
'unique' => false,
'is_configurable' => false,
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
)
);
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'my_custom_attribute_bool', array(
'group' => '',
/** Let type empty in order to enable this attribute filterable , by default the booleans are not allowed
to be filterable, we have to update this custom attribute later to set his backend_type **/
'type' => '',
'backend' => '',
'frontend' => '',
'label' => 'my custom label',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'user_defined' => true, /** Define this property in order to avoid that this attribute is set un general group */
'visible' => true,
'required' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'is_configurable' => false,
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
)
);
/**
* Attributes sets creation
*/
$defaultAttributeSetId = $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, 'default');
$entityTypeId = $installer->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY);
// Attribute sets names
$custom1AttributeSetName = 'Custom1 - Set - Attribute';
$custom2AttributeSetName = 'Custom2 - Set - Attribute';
$attributeSetNames = array(
$custom1AttributeSetName,
$custom2AttributeSetName,
);
// let's create our attributes set
foreach ($attributeSetNames as $attributeSetName) {
$model = Mage::getModel('eav/entity_attribute_set')->setEntityTypeId($entityTypeId);
$model->setAttributeSetName($attributeSetName);
if ($model->validate()) {
$model->save();
$model->initFromSkeleton($defaultAttributeSetId);
$model->save();
}
}
/**
* Attributes binding to attribute sets
*/
foreach($attributeSetNames as $attributeSetName){
//get AttributeSetId for each AttributeSet create above
$attributeSetId = $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, $attributeSetName);
// $groupId can be for example 'General, Prices ...'
// In our case we call method getDefaultAttributeGroupId(), it returns 'general'
$groupId = $installer->getDefaultAttributeGroupId(Mage_Catalog_Model_Product::ENTITY, $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, $attributeSetName));
//OR
//Target directly a attribute_group_id
$groupId = $installer->getAttributeGroupId(Mage_Catalog_Model_Product::ENTITY, $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, $attributeSetName),'my attribute group name');
// If your attribute group is not created yet.
$installer->addAttributeGroup(Mage_Catalog_Model_Product::ENTITY, $attributeSetId, 'my attribute group name', 100);
//add Attribute in the catalog_product entity, in the right attributeSet, in the right Group, with our Custom Attribute Id
$installer->addAttributeToGroup(Mage_Catalog_Model_Product::ENTITY, $attributeSetId, $groupId, $installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'my_custom_attribute'));
}
/**
* Notice :
* instead of use $attributeSetId we can also use directly the customAttributeSetName, Magento will retrieve the right Id
* check Mage_Eav_Model_Entity_Setup
*
* Same thing for attribute. Use attribute name is possible, Magento will retrieve the right Attribute Id
**/
$installer->addAttributeToGroup(Mage_Catalog_Model_Product::ENTITY, $custom1AttributeSetName, $groupId, 'my_custom_attribute');
/** Here we update our boolean attribute **/
$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY,'my_custom_attribute_bool','backend_type','int');
} catch (Exception $e) {
// Silence is golden
Mage::logException($e);
}
<!-- declare resources for setup -->
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<resources>
<namespace_catalog_setup>
<setup>
<module>Namespace_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</namespace_catalog_setup>
</resources>
</global>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment