使用设置脚本在Magento中添加自定义产品属性 [英] Adding custom product attributes in Magento using setup script
问题描述
我正在使用模块设置脚本来添加新的属性组,属性集和属性.我能够创建属性集,属性组并将产品添加到组/集.但是我很难设置 is_filterable , is_visible , is_visible_on_front 和 is_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,属性set($ 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_attribute
和catalog_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",而不仅仅是"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屋!