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
后端的客户表单视图
客户网格和过滤视图
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
有关如何处理此问题或如何检测问题来源的建议的任何人?