2012-07-23 54 views
1

我需要類別中的下拉屬性。我試圖在安裝程序中這樣做:在類別中下拉屬性 - Magento

$installer = $this; 

$installer->startSetup(); 

$entityTypeId = $installer->getEntityTypeId('catalog_category'); 
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId); 
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); 

$installer->addAttribute('catalog_category', 'priority', array(
    'type' => 'varchar', 
    'label' => 'Priority2', 
    'input' => 'select', 
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
    'visible' => true, 
    'required' => false, 
    'user_defined' => false, 
    'option' => array(
      'value' => array(
       0 => array(''), 
       1 => array('normal'), 
       2 => array('grouping'), 
       3 => array('highlighted'), 
       4 => array('trendy'), 
      ), 
     ), 
    'default' => array(0), 
)); 

$installer->addAttributeToGroup(
     $entityTypeId, $attributeSetId, $attributeGroupId, 'priority', '11' 
); 

$attributeId = $installer->getAttributeId($entityTypeId, 'priority'); 

$installer->run(" 
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}` 
(`entity_type_id`, `attribute_id`, `entity_id`, `value`) 
    SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1' 
     FROM `{$installer->getTable('catalog_category_entity')}`; 
"); 

Mage::getModel('catalog/category') 
     ->load(1) 
     ->setImportedCatId(0) 
     ->setInitialSetupFlag(true) 
     ->save(); 

Mage::getModel('catalog/category') 
     ->load(2) 
     ->setImportedCatId(0) 
     ->setInitialSetupFlag(true) 
     ->save(); 

$installer->endSetup(); 

但它不起作用。目錄 - >類別 - >管理類別中沒有任何內容顯示。我只能創建像這樣的textfield屬性:

$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar', 
'label' => 'Priority2', 
'input' => 'text', 
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
'visible' => true, 
'required' => false, 
'user_defined' => false, 
'default' => '', 

));

你有什麼想法嗎? Thx尋求幫助。

編輯。

我也追平SQL查詢是這樣的:

INSERT INTO eav_attribute (entity_type_id, attribute_code, attribute_model, backend_model, backend_type, backend_table, frontend_model, frontend_input, frontend_label, frontend_class, source_model, is_required, is_user_defined, default_value, is_unique, note) VALUES 
    (9, 'priority', NULL, 'NULL', 'int', 'NULL', 'NULL', 'select', 'Priority', NULL, 'eav/entity_attribute_source_table', 1, 0, 0, 0, ''); 
    INSERT INTO eav_attribute_option (option_id,attribute_id,sort_order) VALUES 
    (1500,282,0), 
    (1501,282,1), 
    (1502,282,2), 
    (1503,282,3), 
    (1504,282,4); 
    INSERT INTO eav_attribute_option_value (value_id,option_id,store_id,value) VALUES 
    (201,1500,0,''), 
    (202,1501,0,'normal'), 
    (203,1502,0,'grouping'), 
    (204,1503,0,'highlighted'), 
    (205,1504,0,'trendy'); 

    INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES 
    (9, 12, 7, (SELECT atribute_id FROM eav_attribute WHERE attribute_code='priority'), 2); 
+0

普通的sql查詢應該怎麼做? – diNord 2012-07-23 11:44:22

回答

2

它採用模塊安裝程序可能:

$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'myattribute', array(
    'type' => 'varchar', 
    'label' => 'My attribute', 
    'source' => 'module/category_attribute_myattribute', 
    'backend' => 'module/category_attribute_backend_myattribute', 
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
    'input' => 'select', 
    'visible' => true, 
    'required' => true, 
    'user_defined' => false, 
    'used_in_product_listing' => false, 
    'group' => 'Group name', 
    'default' => Module_Catalog_Model_Category_Attribute_Myattribute::DEFAULT_Groupname, 
)); 

希望它可以幫助別人;)

0

請參考教程文件的結構和一步一步的解釋。 http://www.pearlbells.co.uk/how-to-add-custom-dropdown-attribute-to-magento-category-section/

<?php 
$this->startSetup(); 
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
'group'   => 'General Information', 
'input'   => 'select', 
'type'   => 'text', 
'label'   => 'Custom Dropdown', 
'backend'  => '', 
'visible'  => true, 
'required'  => false, 
'visible_on_front' => true, 
'global'  => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
'source' => 'customfeaturedattribute/source_custom', 
)); 

$this->endSetup(); 

和模型類中添加的選項。

public function getAllOptions() 
{ 
    $options = array(
     1 => 'One', 
     2 => 'Two', 
     3 => 'Three' 
    ); 

    return $options; 
}