Magento - Tạo 1 attribute mới cho category bằng cách update scripts
---o0o---
Để dùng cách này tạo attribute ta cần 1 module mới là AddCategoryAttribute
Đầu tiên trong /app/etc/modules tạo file SM_AddCategoryAttribute.xml để khai báo với Magento
<?xml version="1.0"?>
<config>
<modules>
<SM_Addcategoryattribute>
<active>true</active>
<codePool>local</codePool>
</SM_Addcategoryattribute>
</modules>
</config>
Tiếp theo trong app/local tạo thư mục SM/Addcategoryattribute gồm các file và thư mục con như ảnh dưới
/app/code/local/SM/Addcategoryattribute/etc/config.xml
/app/code/local/SM/Addcategoryattribute/sql/add_category_attribute
Nội dung của /app/code/local/SM/Addcategoryattribute/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<SM_Addcategoryattribute>
<version>0.0.1</version>
</SM_Addcategoryattribute>
</modules>
<global>
<resources>
<add_category_attribute>
<setup>
<module>SM_Addcategoryattribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</add_category_attribute>
<add_category_attribute_write>
<connection>
<use>core_write</use>
</connection>
</add_category_attribute_write>
<add_category_attribute_read>
<connection>
<use>core_read</use>
</connection>
</add_category_attribute_read>
</resources>
</global>
</config>
Code để update scripts ở file /app/code/local/SM/Addcategoryattribute/sql/add_category_attribute/mysql4-install-0.0.1.php
<?php
$this->startSetup();
$this->addAttribute('catalog_category', 'style_select', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Style Select',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'default' => 0,
'source' => 'eav/entity_attribute_source_boolean'
));
$this->endSetup();
?>
Khi muốn thêm 1 attribute tạo file mới có định dạng mysql4-upgrade-0.0.[old]-0.0.[new].php với
[old] là số version cũ
[new] là số version tiếp theo, Tương đương với việc sửa dòng
<version>0.0.[new]</version>
ở file /app/code/local/SM/Addcategoryattribute/etc/config.xml