Skip to content

Instantly share code, notes, and snippets.

@vishy93
Forked from AndresInSpace/My_Module.xml
Created January 25, 2019 15:31
Show Gist options
  • Save vishy93/313e03795ca82635f4b78907a6d60645 to your computer and use it in GitHub Desktop.
Save vishy93/313e03795ca82635f4b78907a6d60645 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>
filepath: app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<active>true</active>
<codePool>local</codePool>
</My_Module>
</modules>
</config>
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment