使用设置脚本在 Magento 中添加自定义产品属性 [英] Adding custom product attributes in Magento using setup script

查看:31
本文介绍了使用设置脚本在 Magento 中添加自定义产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用模块设置脚本来添加新的属性组、属性集和属性.我能够创建属性集、属性组并将产品添加到组/集.但是我很难设置is_filterableis_visibleis_visible_on_frontis_html_allowed_on_front 参数.

I am using module setup script to add new attributes group, attribute set and attributes. I am able to create attribute set, attribute group and add products to group/set. But I am having hard time setting is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters.

$installer->addAttribute('catalog_product', 'offer_type', array(
        'backend'       => '',
        'frontend'      => '',
        'class' => '',
        'default'       => '',
        'label' => 'Offer type',
        'input' => 'text',
        'type'  => 'int',
        'source'        => '',
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'visible'       => 1,
        'required'      => 1,
        'searchable'    => 0,
        'filterable'    => 1,
        'unique'        => 0,
        'comparable'    => 0,
        'visible_on_front' => 1,
        'is_html_allowed_on_front' => 1,
        'user_defined'  => 1,
));

$installer->addAttributeToSet('catalog_product', $sSetId, $groupName, 'offer_type');

我看到 offer_type 被添加到 Magento 和属性集($sSetID)和组($groupname).虽然当我从 magento 管理 UI(目录-> 属性-> 管理属性)查看属性时,我看到 is_filterable、is_visible、is_visible_on_front 和 is_html_allowed_on_front 参数设置为 No.我尝试了各种组合,但没有运气.我正在使用 Magento CE 1.7.0.2.我不确定我的安装脚本中缺少什么.我已经提交了 http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ 为此.我错过了什么吗?提前致谢.

I see offer_type getting added to Magento and to attribute set($sSetID) and to group ($groupname). Though when I look at attribute from magento admin UI (Catalog->attributes->Manage Attributes), I see is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters set to No. I have tried various combinations but no luck. I'm using Magento CE 1.7.0.2. I am not sure what is missing in my setup script. I have reffered http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ for this. Am I missing anything? Thanks in advance.

推荐答案

您是否在 config.xml 中正确配置了安装程序?magento 安装程序的标准类是 Mage_Eav_Model_Entity_Setup 但在处理产品时,您需要使用 Mage_Catalog_Model_Resource_Setup 代替.为什么 ?看看他们的方法_prepareValues(),你就会明白什么是授权属性(产品比标准的eav_objects有更多的选项,你可以在比较表时看到eav_attributecatalog_eav_attribute)

Do you have properly configured your installer in your config.xml ? The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead. Why ? look at their method _prepareValues() and you'll understand what are the authorised attributes (products have more options than the standard eav_objects, you can see that when comparing the tables eav_attribute and catalog_eav_attribute)

要指向好的安装程序类,请查看标准Mage_Catalog config.xml 并使其适合您的模块:

To point to the good installer class, take a look at the standard Mage_Catalog config.xml and adapt it for your module :

<resources>
    <catalog_setup>
        <setup>
            <module>Mage_Catalog</module>
            <class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
        </setup>
    </catalog_setup>
</resources>

ps:注意 _prepareValues() 方法仅在添加属性时调用......如果你想更新一个属性,你需要使用完整的选项名称(is_visible"而不仅仅是可见")...

ps: note that the _prepareValues() method is called only when adding an attribute... if you want to update an attribute you'll need to use the full option name ("is_visible" and not just "visible")...

另一个技巧是在之后添加这些属性,但它不是很漂亮:

Another hack would be to add these attributes afterward, but it's not very beautiful:

// adding atribute :
// [...]

//getting the new attribute with full informations
$eavConfig = Mage::getSingleton('eav/config');
$installer->cleanCache();
$attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
$attribute->addData(array(
    'is_visible' => 1
));
$attribute->save()

这篇关于使用设置脚本在 Magento 中添加自定义产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆