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

1qczuiv0  于 2023-03-12  发布在  其他
关注(0)|答案(3)|浏览(183)

我以编程方式创建了一个新的Product下拉属性(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
            ]
        );

下面是类型选项提供程序类:

<?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;
    }
}

第一个问题:这些选项不会显示在属性页面中:

第二个问题:新添加的选项属性从Magento后端没有显示在属性选项下拉列表中对于这个特定的问题我有一些疑问,我实现getAllOptions()的方式是我得到这个问题的原因。我是新的Magento世界,你能告诉我,我应该如何实现它,以获得新添加的选项。

ctehm74n

ctehm74n1#

我不明白您为什么需要“扩展OptionProvider”
尝试更改为“扩展\Magento\Eav\模型\实体\属性\源\抽象源”

h7appiyu

h7appiyu2#

您可以参考下面的完整示例:https://devdocs.magento.com/videos/fundamentals/add-new-product-attribute/
然而,我遇到了和你的“第一个问题”相同的问题。但是如果你想稍后从下拉菜单中添加/删除选项,你可以更新getAllOptions内部的数组。

nxagd54h

nxagd54h3#

$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' => '',
                'option' => [
                    'values' => [
                        __('typ1')->getText(),
                        __('typ2')->getText(),
                        __('typ3')->getText(),
                        __('typ4')->getText(),
                        __('typ5')->getText()
                    ]
                ]
        );

相关问题