【问题标题】:How to create custom source model for custom attribute type select?如何为自定义属性类型选择创建自定义源模型?
【发布时间】:2013-10-16 18:57:07
【问题描述】:

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

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

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

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

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

谢谢

【问题讨论】:

    标签: magento


    【解决方案1】:

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

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

    $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() 函数的东西。

    【讨论】:

    • 多选没有问题。我可以为多选分配源模型,但不能选择。即使我已经分配了自己的 source_model,它仍然会使用默认的,直到我运行 updateAttribute()。
    • 上面的例子应该适用于任何一个。你能用你的代码更新问题吗?
    【解决方案2】:

    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_model可以在属性创建过程中赋值。看来我必须为所有新属性添加 updateAttribute() 步骤(因为大多数新属性都是选择类型)。还是谢谢。
    【解决方案3】:

    您设置源类型如下所示。

    '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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多