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?
1 Answer 1
Since you just have options Yes/No. Replacing 'input' => 'select' with 'input' => 'boolean' will solve your problem.
Hope it was helpful.
Thanks
- 
 Solved using a different source.Avesh Naik– Avesh Naik2020年09月04日 14:12:33 +00:00Commented Sep 4, 2020 at 14:12
Explore related questions
See similar questions with these tags.