场景
我正在尝试为Magento客户实现自定义属性,该属性应接受布尔值值(真/假,是/否...)。
我正在使用Magento CE 2.2.4。
这是/app/code/TheVendor_TheModule/
下的自定义模块的一部分。
模块的其他组件正常工作。
预期结果
实际结果
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();
}
}
尝试和测试
我尝试了以下操作:
$customerSetup->addAttribute(...)
选项:
'user_defined' => true
。使用时,这会导致属性设置失败而没有错误。'default' => 0
和'default' => '0'
'searchable' => true
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
问题
有人对如何处理此问题或如何发现问题出处提出建议?
答案 0 :(得分:2)
已解决的问题
解决方案:
编辑addAttribute(...)
中的app/code/TheVendor/TheModule/Setup/InstallData.php
在输入'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
上使用源模型select
...
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
'type' => 'int',
'label' => 'Attribute Approved',
/** [Solution] Changed from 'boolean' to 'select' */
'input' => 'select',
/** [Solution] Use source model Boolean */
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'default' => '0',
'required' => false,
'visible' => true,
'system' => false,
'position' => 9,
'sort_order' => 9,
//'user_defined' => true,
//'searchable' => true,
'filterable' => true,
'comparable' => true,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
//'unique' => 0,
]);
...
屏幕
希望这会有所帮助!