我试图搜索这个,但找不到。以编程方式创建具有选择类型的自定义产品属性时,Magento始终将eav/entity_attribute_source_table分配为源模型。
此默认源模型存在 2 个问题:
-
我无法使用从其他地方以编程方式获取的数据自动填充字段,而不必手动逐个键入数据列表。
-
尽管我已经指定了"默认"或"default_value"(我可以在数据库中看到该值在那里),但该字段仍然显示为空作为第一行。
如何将默认source_model更改为我自己的选择类型的源模型?
谢谢
您要查找的密钥是在SQL设置中传递source
值。确保您的$installer
是 EAV 设置对象。
您将在安装脚本中执行以下操作:
$installer = $this;
$installer->starSetup();
// Setup customer multiselect attribute
$attr = array(
'backend' => 'eav/entity_attribute_backend_array',
'input' => 'multiselect',
'label' => 'Permissions',
'note' => 'Used for group-based frontend permissions.',
'required' => false,
'sort_order' => '1000',
'source' => 'eav/entity_attribute_source_table', // Change it here
'user_defined' => true
);
$installer->addAttribute('customer', 'permissions', $attr);
// Add options for permissions
$options = array(
'attribute_id' => $installer->getAttributeId('customer', 'permissions'),
'value' => array(
'place_order' => array('Can Place Orders'),
'view_catalog' => array('Can View the Catalog'),
)
);
$installer->addAttributeOption($options);
$installer->endSetup();
通常,我相信源模型可以是提供toOptionArray()
函数的任何内容。
在 Mage_Customer
中有一个很好的例子,安装程序:mysql4-upgrade-1.5.9.9-1.6.0.0.php
在其中,国家/地区源模型正在分配给客户地址属性country_id
。
$installer->updateAttribute(
'customer_address',
'country_id',
'source_model',
'customer/entity_address_attribute_source_country'
);
将其更改为catalog_product、属性和源模型。
您可以按如下所示设置源类型。
'source' => 'categoryattr/attribute_source_type',
并创建文件属性\源\类型.php 和创建选项并将值设置为 0 作为默认选项。
$this->_options[] = array (
'label' => 'Select Category',
'value' => '0'
);
有关文件结构和分步说明,请参阅下文。
http://www.pearlbells.co.uk/how-to-create-custom-attribute-source-type-in-magento/