未显示Magento 2自定义下拉属性添加的选项

时间:2018-07-31 17:40:56

标签: magento magento2

我以编程方式创建了一个新的“产品”下拉属性(Typ):

    $eavSetupFactory->addAttribute(
            Product::ENTITY,
            $name,
            [
                'type' => 'int',
                'label' => $name,
                'input' => 'select',
                'required' => true,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'searchable' => true,
                'filterable' => true,
                'filterable_in_search' => true,
                'comparable' => false,
                'visible_on_front' => true,
                'unique' => false,
                'group' => 'General',
                'is_used_in_grid' => true,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => true,
                'user_defined' => true,
                'set' => 'Default',
                'source' => TAC\Migration\Setup\Configuration\Source\TypOptionProvider::class
            ]
        );

这是TypOptionProvider类:

    <?php
namespace BAG\Migration\Setup\Configuration\Source;
class TypOptionProvider extends OptionProvider
{
protected $eavConfig;

public function __construct( \Magento\Eav\Model\Config $eavConfig){
    $this->eavConfig = $eavConfig;
}

public function getAllOptions()
{
    $factory = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'Typ');

    if (!$this->_options) {
        $this->_options = [
            ['label' => __('typ1'), 'value' => 1],
            ['label' => __('typ2'), 'value' => 2],
            ['label' => __('typ3'), 'value' => 3],
            ['label' => __('typ4'), 'value' => 4],
            ['label' => __('typ5'), 'value' => 5]
        ];
    }
    return $this->_options;
    }
}

第一个问题:这些选项未显示在属性页面中: enter image description here

第二个问题:从Magento后端为属性添加的新选项未显示在属性选项下拉列表中 对于这个特定的问题,我有些怀疑,我实现getAllOptions()的方式就是我遇到此问题的原因。我是Magento世界的新手,请告诉我如何实现它才能获得新添加的选项。

enter image description here

2 个答案:

答案 0 :(得分:0)

我不明白您为什么需要“扩展OptionProvider”

尝试更改为“扩展\ Magento \ Eav \ Model \ Entity \ Attribute \ Source \ AbstractSource”

答案 1 :(得分:0)

您可以在此处参考完整的示例: https://devdocs.magento.com/videos/fundamentals/add-new-product-attribute/

但是,我遇到的问题与您的“第一个问题”相同。但是,如果您以后想从下拉菜单中添加/删除选项,则可以更新getAllOptions中的数组。