有时候你需要扩展Magento的类别功能。有几种方法你可以达到目的,你可以直接修改和添加数据到表里,但如果你不知道怎么做的话就会浪费时间。这篇文章将演示如何通过sql_setup脚本来给你的Magento店铺添加新的自定义类别属性。

示例Mysql setup脚本是这样的,你需要根据你的需求来修改新属性的配置。

$installer = $this;
$installer->startSetup();
 
$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
 
$installer->addAttribute('catalog_category', 'new_cat_attrb',  array(
    'type'     => 'int',
    'label'    => 'New Category Attribute',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0
));
 
 
$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'new_cat_attrb',
    '11'					//last Magento's attribute position in General tab is 10
);
 
$attributeId = $installer->getAttributeId($entityTypeId, 'new_cat_attrb');
 
$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')}`;
");
 
 
//this will set data of your custom attribute for root category
Mage::getModel('catalog/category')
    ->load(1)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();
 
//this will set data of your custom attribute for default category
Mage::getModel('catalog/category')
    ->load(2)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();
 
$installer->endSetup();

在你模块的config.xml中你需要添加下面的代码来使新的类别属性正确安装:

<resources>
	<new_attribute_csv_setup>
	  <setup>
		<module>New_Attribute</module>
		<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
	  </setup>
	  <connection>
		<use>core_setup</use>
	  </connection>
	</new_attribute_setup>
	<new_attribute_setup_write>
	  <connection>
		<use>core_write</use>
	  </connection>
	</new_attribute_setup_write>
	<new_attribute_setup_read>
	  <connection>
		<use>core_read</use>
	  </connection>
	</new_attribute_setup_read>
</resources>

注意,这里的类标签必须是 “Mage_Catalog_Model_Resource_Eav_Mysql4_Setup”

正确安装后,进入后台类别中,你可以看到在General Information中出现了"New Category Attribute"。

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系