设想
我正在尝试为 Magento 客户实现一个自定义属性,它应该接受布尔值(True/False,Yes/No...)。
我正在使用 Magento CE 2.2.4。
这是自定义模块的一部分/app/code/TheVendor_TheModule/
。
模块的其他组件工作正常。
预期结果
- 属性必须用后端客户表单中的开关输入或复选框来表示。
- 属性及其值必须出现在客户网格中
- 该属性必须出现在带有可选选项的过滤器中(是/否或真/假或是/不是,任何类似布尔值都可以正常工作)
实际结果
- [ OK ] 一个开关按预期显示在客户表单的后端。
- [确定]将开关值更改为开或关+保存工作正常。
- [问题] 属性
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 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
测试程序
对于我进行的每项测试,我都遵循以下步骤以确保正确安装模块:
- 执行
php bin/magento module:disable TheVendor_TheModule
- 从数据库中删除记录:
- 删除模块记录
mage_setup_module
- 删除 EAV 记录
mage_eav_attribute
- 删除模块记录
- 确保模块在
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
问题
有人对如何处理此问题或如何检测问题出在哪里提出建议吗?