首页 > 解决方案 > Magento 2多选自定义产品属性选项未显示

问题描述

在我的自定义模块中,使用 installData.php 创建自定义多选属性。我从我的源类中设置了选项值(使用 Magento\Eav\Model\Entity\Attribute\Source\AbstractSource),安装后工作正常。我可以在编辑产品时看到选项。

但是在编辑属性时选项不可见。在此之后我无法添加/删除选项。

请指教。

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'my_option',
        [
            'group' => 'General',
            'label' => 'My Label',
            'type'  => 'text',
            'input' => 'multiselect',
            'user_defined' => true,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'source' => 'Vendor\Module\Model\Attribute\Source\Options',
            'required' => false,
            'filterable' => true,
            'filterable_in_search' => true,
            'is_searchable_in_grid' => false,                
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false, 
            'sort_order' => 200,
            'used_in_product_listing' => true,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'visible' => true,
            'visible_on_front' => true,
            'searchable' => false,
            'comparable' => false,
        ]
    );

标签: attributesmagento2productadminhtml

解决方案


1. 在 Vendor\Extension\Setup\ 文件夹中创建 InstallData.php 文件。

<?php

namespace Vendor\Extension\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'eway_option',
            [
                'group' => 'Groupe Name',
                'label' => 'Multiselect Attribute',
                'type'  => 'text',
                'input' => 'multiselect',
                'source' => 'Vendor\Extension\Model\Config\Product\Extensionoption',
                'required' => false,
                'sort_order' => 30,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'used_in_product_listing' => true,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'visible_on_front' => false
            ]
        );
        $setup->endSetup();
    }
}

2. 在 Vendor\Extension\Model\Config\Product 文件夹中创建 Extensionoption.php 文件。

<?php
 
namespace Vendor\Extension\Model\Config\Product;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
 
class Extensionoption extends AbstractSource
{
    protected $optionFactory;
    public function getAllOptions()
    {
        $this->_options = [];
        $this->_options[] = ['label' => 'Label 1', 'value' => 'value 1'];
        $this->_options[] = ['label' => 'Label 2', 'value' => 'value 2'];
    
        return $this->_options;
    }
}

推荐阅读