Skip to content

Instantly share code, notes, and snippets.

@AndresInSpace
Last active June 17, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndresInSpace/ec6e88e1c627c6b012356a31206f7769 to your computer and use it in GitHub Desktop.
Save AndresInSpace/ec6e88e1c627c6b012356a31206f7769 to your computer and use it in GitHub Desktop.
Magento Product Attribute Setup Installer - Add New Product Attribute & Add to All Attribute Sets to Specified Group - Full List of available params used for catalog_product addAttribute installation
filepath: My/Module/etc/
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<resources>
<unique_resource_name>
<setup>
<module>My_Module</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</unique_resource_name>
<unique_resource_name>
<connection>
<use>core_write</use>
</connection>
</unique_resource_name>
<unique_resource_name>
<connection>
<use>core_read</use>
</connection>
</unique_resource_name>
</resources>
</global>
</config>
<?php
/**
* Product Attribute Installer
*/
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'new_attribute', array(
'group' => 'Display Settings', ///the tab group it belongs to
'input' => 'select', //input type
'type' => 'int', //db type
'label' => 'Show Simple Products Only', //backend label
'source' => 'eav/entity_attribute_source_boolean', //source for options if needed, else empty/null it
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, // scope to store or ::GLOBAL
'backend' => '', //backend model if needed for options, otherwise we leave empty/null
'wysiwyg_enabled' => 0, //no we hate this
'visible_on_front' => 1, //include in collection data
'is_html_allowed_on_front' => 0, //not sure if this is required or not, seems the table is shared with product attributes that use this
'visible' => 1, //is visible
'required' => 0, //is required?
'user_defined' => 1, //user defined yes
'default' => '0', //default value - I am using a select int dropdown (yes/no) bool, so I'm defaulting to No(0).
));
$installer->endSetup();
filepath: My/Module/sql/unique_resource_name/
<?php
/**
* Product Attribute Installer
*/
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$code = 'new_attribute_code';
$installer = $this;
$installer->startSetup();
// Add new attribute `size`
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $code, array(
'label' => 'MyLabel', // Default label
'input' => 'text', // Input type (text, textarea, select...)
'type' => 'varchar', // Attribute type (varchar, text, int, decimal...)
'required' => false, // Is the attribute mandatory?
'comparable' => false, // Is the attribute comparable? (on frontend).
'filterable' => false, // Is the attribute filterable? (on frontend, in category view)
'filterable_in_search' => false, // Is the attribute filterable? (on frontend, in search view)
'used_for_promo_rules' => false, // Do we need that attribute for specific promo rules?
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, // Attribute scope
'is_configurable' => false, // Can the attribute be used to create configurable products?
'is_html_allowed_on_front' => false, // Is HTML allowed on frontend?
'note' => '', // Note below the input field on admin area
'searchable' => false, // Is the attribute searchable?
'unique' => false, // Must attribute values be unique?
'used_for_sort_by' => false, // Can the attribute be used for the 'sort by' select on catalog/search views?
'used_in_product_listing' => false, // Attribute value included in product model for catalog listing
'user_defined' => true, // Is the attribute user defined? If false the attribute isn't removable. TRUE needed if configurable attribute.
'visible' => true, // Is the attribute visible? If true the field appears in admin product page.
'visible_on_front' => true, // Is the attribute visible on front?
'visible_in_advanced_search' => false, // Is the attribute visible on advanced search?
'wysiwyg_enabled' => false // Is Wysiwyg enabled? (use `textarea` input if you put that value to true)
));
//add to all available attribute_sets, to specified group
$entityId = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code', 'catalog_product')->getFirstItem()->getId();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityId);
foreach ($collection as $attrs):
$installer->addAttributeToSet(
Mage_Catalog_Model_Product::ENTITY, // Entity type
$attrs->getAttributeSetId(), // Attribute set name
'Product Details', // specified Attribute set group name you want to add to
$code, // Attribute code to add
5 // Position on the attribute set group
);
endforeach;
$installer->endSetup();
filepath: app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<active>true</active>
<codePool>local</codePool>
</My_Module>
</modules>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment