且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento:使用模块安装脚本添加产品属性

更新时间:2023-11-30 09:38:52

首先,这不是有效的config.xml.设置类的配置如下:

First, this is not a valid config.xml. The setup class is configured as follows:

<config>
    ...
    <global>
        ...
        <resources>
            ...
            <your_module_setup>
                <setup>
                    <module>Your_Module</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
            </your_module_setup>
            ...
        </resources>
        ...
    </global>
    ...
</config>

您也可以使用自己的安装程序类来代替Mage_Eav_Model_Entity_Setup,但是它应该继承Mage_Eav_Model_Entity_Setup,因此您可以使用addAttribute代替手工伪造SQL查询.

instead of Mage_Eav_Model_Entity_Setup you could also use your own setup class but it should inherit Mage_Eav_Model_Entity_Setup, so you can use addAttribute instead of forging the SQL queries by hand.

然后您的设置脚本应类似于以下内容:

Then your setup script should look similar to this:

$installer = $this;
$installer->startSetup();
/*
 * adds product unit attribute to product
 */
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'productunit_id', array(
    'label' => Mage::helper('productunits')->__('Quantity Unit'),
    'type' => 'int',
    'input' => 'select',
    'source' => SGH_ProductUnits_Model_Entity_Attribute_Source_Units::MODEL,
    'backend' => SGH_ProductUnits_Model_Entity_Attribute_Backend_Units::MODEL,
    'required' => 1,
    'global' => 1,
    'note' => Mage::helper('productunits')->__('This will be displayed next to any Qty value.')
));
$installer->endSetup();

这是我的代码,添加了数量单位属性,不要被类常量的使用所迷惑,它们只是对应的类别名.

It's my code that adds a quantity unit attribute, don't be confused by the use of class constants, those are just the corresponding class aliases.