2012-10-17 62 views
7

我正在使用模块设置脚本添加新的属性组,属性集和属性。我能够创建属性集,属性组和添加产品到组/集。但我有很难设定is_filterableis_visibleis_visible_on_frontis_html_allowed_on_front参数。使用安装脚本在Magento中添加自定义产品属性

$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)和组($组名)。虽然当我从magento管理界面(目录 - >属性 - >管理属性)查看属性时,我看到is_filterable,is_visible,is_visible_on_front和is_html_allowed_on_front参数设置为否。我尝试了各种组合,但没有运气。我正在使用Magento CE 1.7.0.2。我不确定我的设置脚本中缺少什么。我已经为此回复了http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/。我错过了什么? 在此先感谢。

回答

7

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

指向好安装程序类,取一看标准Mage_Catalog​​3210和适应它为你的模块:

<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”而不只是“可见”)...

另劈将是后来添加这些属性,但它不是很漂亮:

// 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() 
+2

谢谢!问题解决了。 – sukkad

+0

任何想法如何设置is_visible_on_front?尽管我在addAttribute调用中将其设置为1('is_visible_on_front'=> 1,但我没有看到它在DB/admin中被更改。 – sukkad

-2

使用'visible_on_front' => 1,在addAttribute通话。

相关问题