Magento 为产品添加新属性

Magento add new attribute to product

我正在尝试为所有产品添加一个新属性,在阅读了很多之后我得到了这段代码:

<?php

$installer = $this;
$installer->startSetup();

$attribute = array(
    'group'             => 'General',
    'type'              => 'boolean',
    'source'            => 'eav/entity_attribute_source_boolean',
    'sort_order'        => 7,
    'label'             => 'producto destacado',
    'input'             => 'select',
    'class'             => 'validate-number',
    'backend'           => '',
    'frontend'          => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible_on_front'  => true,
    'visible_in_advanced_search' => true,
    'unique'            => false,
);

$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'is_featured', $attribute);
$installer->endSetup();

但这让我犯了一个错误:

Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() in /Applications/MAMP/htdocs/app/code/local/Feliu/Featuredproducts/sql/feliu_featuredproducts_setup/install-0.1.0.php on line 34

所以 $installer->addAttribute() 不起作用,我不确定为什么。我昨天在所有类别中添加了一个属性并且成功了。

我之前尝试过使用 $installer->addAttribute('catalog_product', 'is_featured', $attribute);,但我读到有关使用 Mage_Catalog_Model_Product::ENTITY 而不是 catalog_product 的信息,因此我更改了它。我在他们身上看到了同样的错误信息。

我想我遗漏了一些重要的东西,但现在我看不到它是什么。 :-S

PD:我使用的是 magento 1.9.3

您使用的安装模型有误。 Mage_Core_Model_Resource_Setup确实没有addAttribute方法。

您应该使用 Mage_Catalog_Model_Resource_Setup 为产品添加属性。您可以更改 config.xml 文件中使用的设置模型:

<?xml version="1.0"?>
<config>
    <global>
        ...
        <resources>
            <namespace_module_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
            </namespace_module_setup>
        </resources>
        ...
    </global>
</config>

希望这对您有所帮助。 :)