Error after creating boolean attribute magento2
-
18-02-2021 - |
Question
I have created a boolean attribute using below code.
$setup = $this->_objectManager->get('Magento\Framework\Setup \ModuleDataSetupInterface');
$eav_setup_factory = $this->_objectManager->get('Magento\Eav\Setup\EavSetupFactory');
$setup->startSetup();
$eavSetup = $eav_setup_factory->create(['setup' => $setup]);
$AttributeCode = "test_attribute";
$AttributeName = "Test Attribute";
$type = 'int';
$input_type = 'boolean';
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
$AttributeCode,
[
'type' => $type,
'backend' => '',
'frontend' => $AttributeName,
'label' => $AttributeName,
'input' => $input_type,
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
Attribute created but when try to open I am getting this error.
Class "Test Attribute" does not exist.
can anyone look into this issue and update me please.
I need to create Yes/No attribute for product.
Solution
First of all, $AttributeName
is not defined. and you use that variable for frontend
and label
.
I assume you changed your code before posting it here and you have somewhere $AttributeName = 'Attribute label'
.
You need to remove 'frontend' => $AttributeName,
. Maybe replace it with 'frontend' => '',
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange