Creating attribute sets and attributes programmatically

后端 未结 3 1946
时光取名叫无心
时光取名叫无心 2021-01-16 06:23

I am using the code listed on the following link:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_att

3条回答
  •  遥遥无期
    2021-01-16 07:00

    You will need to have a block in your modules config.xml that looks like so

    
        
            
                Namespace_Module
                Mage_Eav_Model_Entity_Setup
            
        
     
    

    This will allow you to place your installer code in the directory that is in your XML. You need to make sure that the version listed on the installer file matches the 1.2.0 of your module otherwise Magento will not be able to run your installer. To add an attribute Set, you can use the following data, I have never use it, but the entityTypeId defines whether it is a customer, shipping, category, product entity, each 1, 2, 3, 4 respectively.

    /**
         * Add Attribute Set
         *
         * @param mixed $entityTypeId
         * @param string $name
         * @param int $sortOrder
         * @return Mage_Eav_Model_Entity_Setup
         */
        public function addAttributeSet($entityTypeId, $name, $sortOrder = null)
        {
            $data = array(
                'entity_type_id'        => $this->getEntityTypeId($entityTypeId),
                'attribute_set_name'    => $name,
                'sort_order'            => $this->getAttributeSetSortOrder($entityTypeId, $sortOrder),
            );
    
            $setId = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id');
            if ($setId) {
                $this->updateAttributeSet($entityTypeId, $setId, $data);
            } else {
                $this->_conn->insert($this->getTable('eav/attribute_set'), $data);
    
                $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName);
            }
    
            return $this;
        }
    

    This is the code for adding an attribute to a set, just change the attribute set data

    //app/code/local/Namespace/Module/sql/Namespace_Module_setup/mysql4-install-1.0.0.php
        $installer = $this;
        /* @var $installer Mage_Eav_Model_Entity_Setup */
    
        $installer->startSetup();
    
            $data= array (
                'attribute_set' =>  'Default',
                'group' => 'General',
                'label'    => 'Some Label',
                'visible'     => true,
                'type'     => 'varchar', // multiselect uses comma-sep storage
                'input'    => 'text',
                'system'   => true,
                'required' => false,
                'user_defined' => 1, //defaults to false; if true, define a group
            );
    
            $installer->addAttribute('catalog_product','attriute_code',$data)
    
            $installer->endSetup();
    

    The above is a working example of an attribute installation for a module.

提交回复
热议问题