attributes - 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,
]
);
解决方案
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;
}
}
推荐阅读
- swift - 表达式类型不明确,没有更多上下文 xcode 11
- reactjs - 如何更改antd上传组件中的上传文本?
- python - “如何在一行上打印多个用户输入”
- c# - 如何将两种方法与不同的对象结合起来(两种方法中的代码相同)
- javascript - JavaScript 似乎没有比前几行更进一步?
- amazon-s3 - 为 Kafka 调整 S3 文件大小
- outlook - 可固定 Outlook 任务窗格 - Outlook 网页版
- javascript - 如何编写通用的可重用承诺 then/catch 处理程序?
- sql - 使用 Join 和 Group By 优化此查询?
- c++ - 稀疏对称矩阵初始化