2

我试图搜索这个但找不到任何东西。当以编程方式创建具有选择类型的自定义产品属性时,Magento 始终将eav/entity_attribute_source_table分配为源模型。

此默认源模型存在 2 个问题:

  1. 我无法使用从其他地方以编程方式获取的数据自动填充该字段,而必须一一手动键入数据列表。

  2. 尽管我已经指定了“default”或“default_value”(我可以在数据库中看到该值存在),但该字段仍然显示为空作为第一行。

如何将默认 source_model 更改为我自己的源模型以供选择类型?

谢谢

4

3 回答 3

3

您正在寻找的关键是source在您的 SQL 设置中传递一个值。确保您$installerEAV 设置对象

您将在安装脚本中执行以下操作:

$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()功能的东西。

于 2013-10-16T23:50:12.563 回答
0

安装程序中有一个很好的例子Mage_Customermysql4-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、您的属性和源模型。

于 2013-10-16T19:02:21.580 回答
0

您设置源类型,如下所示。

'source'        => 'categoryattr/attribute_source_type',

并创建文件 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/

于 2016-11-03T11:32:16.560 回答