0

I have created a custom attribute in magento 2 for product using a source Magento\Eav\Model\Entity\Attribute\Source\Boolean

Here is the code

     if(!$eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, 'purchase_unit')) {
        $eavSetup->addAttribute(
            Product::ENTITY,
            'purchase_unit',
            [
                'group' => 'Rocket',
                'type' => 'int',/* Data type in which formate your value save in database*/
                'backend' => '',
                'frontend' => '',
                'label' => 'Purchase Unit', /* lablel of your attribute*/
                'input' => 'select',
                'class' => '',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                /*Scope of your attribute */
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '0',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );
    }

The attribute is getting created and also displays in product page. The issue is it displays YES as default value which is 1. How to solve this problem?

0

Since you just have options Yes/No. Replacing 'input' => 'select' with 'input' => 'boolean' will solve your problem.

Hope it was helpful.

Thanks

1
  • Solved using a different source. – Avesh Naik Sep 4 '20 at 14:12

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.