【发布时间】:2018-12-26 04:03:43
【问题描述】:
场景
我正在尝试为 Magento 客户实现一个自定义属性,该属性应该接受 boolean 值(True/False、Yes/No...)。
我正在使用 Magento CE 2.2.4。
这是/app/code/TheVendor_TheModule/ 下自定义模块的一部分。
模块的其他组件工作正常。
预期结果
- 该属性必须用后端客户表单中的开关输入或复选框来表示。
- 属性及其值必须出现在客户网格中
- 属性必须出现在 过滤器 中并带有可选选项(是/否或真/假或是/不是,任何类似布尔值都可以正常工作)
实际结果
- [OK] 一个开关按预期显示在客户表单的后端。
- [OK] 将开关值更改为 on 或 off + 保存可以正常工作。
- [问题] 属性的
Label显示在客户网格中,但缺少值。 - [问题] 过滤器中的属性输入显示,但不包含选项。
屏幕
后端的客户表单视图
客户网格和过滤视图
代码
<?php
namespace TheVendor\TheModule\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
const ATTRIBUTE_APPROVED = 'attribute_approved';
protected $customerSetupFactory;
private $eavSetupFactory;
private $eavConfig;
private $attributeResource;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
EavSetupFactory $eavSetupFactory,
Config $eavConfig,
\Magento\Customer\Model\ResourceModel\Attribute $attributeResource
){
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeResource = $attributeResource;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
'type' => 'int',
'label' => 'Attribute Approved',
'input' => 'boolean',
'required' => false,
'visible' => true,
'system' => false,
'position' => 9,
'sort_order' => 9,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
//'user_defined' => true, //commented because causing attribute fail on module install
//'searchable' => true,
'filterable' => true,
'comparable' => true,
'default' => '0',
//'unique' => 0,
]);
$myAttribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED);
$myAttribute->setData('used_in_forms', ['adminhtml_customer']);
$this->attributeResource->save($myAttribute);
$setup->endSetup();
}
}
尝试和测试
我尝试了以下方法:
- Magento 开发文档中的查找解决方案
- StackExchange 上的查找解决方案
- 在其他论坛上查找解决方案
- 调整
$customerSetup->addAttribute(...)选项:- 设置
'user_defined' => true。使用时,这会导致属性设置失败且没有错误。 - 设置
'default' => 0和'default' => '0' - 设置
'searchable' => true
- 设置
- 检查日志是否有错误,未找到。
- 删除 Module 文件夹并在重新安装之前重新创建它
- 已执行
php bin/magento setup:di:compile - 已执行
php bin/magento setup:static-content:deploy -f
测试程序
对于我进行的每个测试,我都遵循以下步骤以确保正确安装模块:
- 执行
php bin/magento module:disable TheVendor_TheModule - 从数据库中删除记录:
- 删除
mage_setup_module中的模块记录 - 删除
mage_eav_attribute中的EAV记录
- 删除
- 确保模块在
app/etc/config.php中被禁用 - 拉取更新的代码
- 执行
php bin/magento module:enable TheVendor_TheModule - 执行
php bin/magento setup:upgrade - 执行
php bin/magento indexer:reindex - 执行
php bin/magento cache:clean
问题
有人对如何处理此问题或如何检测问题的根源提出建议吗?
【问题讨论】:
标签: backend custom-attributes entity-attribute-value magento2.2