0

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.

2

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' => '',

  • hi @Marius, sorry its my mistake, updated my question – jafar pinjar Feb 13 at 9:06
  • so my suspicion was right. My answer stands – Marius Feb 13 at 9:24
  • no, I forgot to add that variable in question, attribute created successfully, once it is clicked in admin, i am getting that error, – jafar pinjar Feb 13 at 9:26
  • Read my answer carefully. You need to remove the line 'frontend' => $AttributeName or replace it with 'frontend' => ''. Delete your attribute and recreate it again with my suggestion. – Marius Feb 13 at 9:48
  • hi @marius, then how to update the label for attribute if removed frontend? – jafar pinjar Feb 13 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.