java eav_使用EAV的客户自定义属性不在Grid中显示值

Scenario

我正在尝试为Magento Customer实现一个自定义属性,该属性应该接受 boolean 值(True / False,Yes / No ...) .

我正在使用Magento CE 2.2.4 .

这是 /app/code/TheVendor_TheModule/ 下的自定义模块的一部分 .

模块的其他组件正常工作 .

Expected Result

该属性必须在后端客户表单中使用 switch 输入或复选框表示 .

属性及其值必须出现在Customers Grid中

该属性必须出现在带有可选选项的 Filter 中(是/否或真/假或是/否,任何类似布尔值的工作正常)

Actual Result

[确定]开关按预期显示在客户表单的后端 .

[确定]将开关值更改为开启或关闭保存工作正常 .

[ Issue ]属性 Label 显示在 Customer Grid 但 the values are missing 中 .

[ Issue ] Filter 中的属性输入显示但是 does not contain options .

Screens

后端的客户表单视图

69dbd282-447c-4685-857b-11d36df833f8.png

客户网格和过滤视图

2d4b1252-5bf8-422e-a234-8da9853bbf9d.png

Code

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();

}

}

Attempts and Tests

我尝试了以下方法:

Magento Dev Docs中的查找解决方案

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

Testing Routine

对于我所做的每个测试,我都遵循这些步骤以确保正确安装模块:

执行 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

Question

有关如何处理此问题或如何检测问题来源的建议的任何人?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值