【问题标题】:Create dependant attributes in install script in Magento在 Magento 的安装脚本中创建依赖属性
【发布时间】:2014-01-23 11:21:54
【问题描述】:

在 Magento 的管理区域中,我正在尝试创建一个依赖字段。依赖字段是一个仅根据其值变为可用/启用的字段,例如,带有“是”或“否”值的下拉列表。这是 Magento 的内置功能,您可以从 this blog post 看到。

但是,上面的博客文章(以及我发现的其他文章)假设字段被添加到 system.xml 或使用下面概述的方法 Vikram 但我想在定义我的属性时在我的模块安装脚本中添加我的依赖项,例如:

$installer->addAttribute(
    'catalog_category', 
    'show_dependant', 
    array(
        'label' => 'Show dependant?',
        'group' => 'My Group',
        'type' => 'int',
        'input' => 'select',
        'source'  => 'eav/entity_attribute_source_boolean',
        'required' => false,
        'visible' => true,
        'default' => '0',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    )
);


$installer->addAttribute(
    'catalog_category',
    'my_attribute_name',
    array(
        'label' => 'A New Attribute',
        'group' => 'My Group',   //will be created if necessary
        'type'  => 'int',
        'class' => 'validate-number',
        'required' => false,

        // Would be something like this maybe?
        'depends' => array('show_dependant', 1)

    )
);

有人知道这是否可能吗?

【问题讨论】:

    标签: magento


    【解决方案1】:

    如果您在 MAGENTO ADMIN FORMS 中工作,请考虑仅在选择“指定”选项时才显示文本字段的示例。此方法使用的是管理表单,而不是 system.xml 方法。

    $form = new Varien_Data_Form();
    
    $form->addField('yesno', 'select', array(
        'label'  => $this->__('Yes or No?'),
        'values' => Mage::model('adminhtml/system_config_source_yesnocustom')
            ->toOptionArray(),
    ));
    $form->addField('custom_value', text, array(
        'label'  => $this->__('Other'),
    ));
    
    // Append dependency javascript
    $this->setChild('form_after', $this->getLayout()
        ->createBlock('adminhtml/widget_form_element_dependence')
            ->addFieldMap('yesno', 'yesno')
            ->addFieldMap('custom_value', 'custom_value')
            ->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'
    );
    

    您可以通过这种方式添加任意数量的字段映射和字段依赖项。

    【讨论】:

    • 您好,感谢您的帮助。我知道我可以通过这种方式添加依赖项,但是我的属性是通过我在提出以下问题后创建的安装脚本添加的:magento.stackexchange.com/questions/13255/…
    • Magento 在使用安装脚本时是否支持字段依赖?
    • 正是我的问题:)
    • Mage 使用 Varien_Db_Ddl_Table 类,您可以在其中配置所有字段和键。检查所有方法并毫不犹豫地查看您将要使用的类定义,您可以为自己找到很多有趣的东西:)
    【解决方案2】:

    我通过为属性添加新的输入渲染器创建了简单的类别属性依赖项。它是这样工作的: 你有几个属性:

    – my_attribute
    – my_attribute_text
    – my_attribute_select
    

    请注意,它们都是从 my_attribute 开始的。

    第一个属性具有布尔类型。当它设置为 true 时 - 从 my_attribute 开始的其他属性可见。

    来源 - https://github.com/elpas0/category_dependence

    描述 - http://nwdthemes.com/2015/02/20/magento-category-attributes-dependency/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2015-09-18
      • 2011-06-09
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多