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?

asked Sep 2, 2020 at 11:06

1 Answer 1

0

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

Hope it was helpful.

Thanks

answered Sep 3, 2020 at 8:02
1
  • Solved using a different source. Commented Sep 4, 2020 at 14:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.