Not sure why it took so long to figure out, but adding a YES/NO attribute to categories actually turned out to be pretty easy.
I wanted a quick way to set a category as “featured”.
So, I created a quick setup script to build it.
Option.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
class Branded3_Addattribute_Model_Entity_Attribute_Source_Option extends Mage_Eav_Model_Entity_Attribute_Source_Abstract { /** * Retrieve all options array * * @return array */ public function getAllOptions() { if (is_null($this->_options)) { $this->_options = array( array( 'label' => Mage::helper('eav')->__('Yes'), 'value' => 1 ), array( 'label' => Mage::helper('eav')->__('No'), 'value' => 0 ), ); } return $this->_options; } /** * Retrieve option array * * @return array */ public function getOptionArray() { $_options = array(); foreach ($this->getAllOptions() as $option) { $_options[$option['value']] = $option['label']; } return $_options; } /** * Get a text for option value * * @param string|integer $value * @return string */ public function getOptionText($value) { $options = $this->getAllOptions(); foreach ($options as $option) { if ($option['value'] == $value) { return $option['label']; } } return false; } /** * Retrieve flat column definition * * @return array */ public function getFlatColums() { $attributeCode = $this->getAttribute()->getAttributeCode(); $column = array( 'unsigned' => false, 'default' => null, 'extra' => null ); if (Mage::helper('core')->useDbCompatibleMode()) { $column['type'] = 'tinyint(1)'; $column['is_null'] = true; } else { $column['type'] = Varien_Db_Ddl_Table::TYPE_SMALLINT; $column['length'] = 1; $column['nullable'] = true; $column['comment'] = $attributeCode . ' column'; } return array($attributeCode => $column); } /** * Retrieve Indexes(s) for Flat * * @return array */ public function getFlatIndexes() { $indexes = array(); $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode()); $indexes[$index] = array( 'type' => 'index', 'fields' => array($this->getAttribute()->getAttributeCode()) ); return $indexes; } /** * Retrieve Select For Flat Attribute update * * @param int $store * @return Varien_Db_Select|null */ public function getFlatUpdateSelect($store) { return Mage::getResourceModel('eav/entity_attribute') ->getFlatUpdateSelect($this->getAttribute(), $store); } } |
Setup.php
1 2 3 4 5 |
/** * Setup */ class Branded3_Addattribute_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup { } |
Config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
< ?xml version="1.0"?> <config> <modules> <branded3_addattribute> <version>1.0</version> </branded3_addattribute> </modules> <frontend> <routers> <addattribute> <use>standard</use> <args> <module>Branded3_Addattribute</module> <frontname>addattribute</frontname> </args> </addattribute> </routers> </frontend> <global> <resources> <addattribute_setup> <setup> <module>Branded3_Addattribute</module> <class>Branded3_Addattribute_Model_Resource_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </addattribute_setup> </resources> </global> </config> |
mysql4-install-1.0.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$installer = $this; $installer->startSetup(); $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $entityTypeId = $setup->getEntityTypeId('catalog_product'); $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); $attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); $setup->addAttribute('catalog_category', 'highlighted_category', array( 'type' => 'int', 'group' => 'General Information', 'backend' => '', 'frontend' => '', 'label' => 'Highlighted Category', 'input' => 'select', 'class' => '', 'source' => 'eav/entity_attribute_source_boolean', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '0', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'unique' => false, )); $setup->addAttributeToGroup( $entityTypeId, $attributeSetId, $attributeGroupId, 'highlighted_category', '11' ); $installer->endSetup(); |
Image Credit: Aristocrats-hat
Comment or tweet @douglasradburn