我尝试搜索此内容,但没有找到任何内容。以编程方式创建具有选择类型的自定义产品属性时,Magento 始终将 eav/entity_attribute_source_table 指定为源模型。
此默认源模型有 2 个问题:
除了必须手动逐一键入数据列表之外,我无法使用从其他地方以编程方式获取的数据自动填充该字段。
虽然我已经指定了“default”或“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',
并创建文件 Attribute\Source\Type.php 并创建选项并将默认选项值设置为 0。
$this->_options[] = array (
'label' => 'Select Category',
'value' => '0'
);