I want to Create a product's custom attribute with 'yes/no' drop-down into my custom module.
Please suggest any solution.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want to Create a product's custom attribute with 'yes/no' drop-down into my custom module.
Please suggest any solution.
Step-1: Create a InstallData.php file with below script
VendorName/ModuleName/Setup/InstallData.php.
<?php
namespace VendorName\ModuleName\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'custom_attribute',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Custom Label',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
]
);
}
}
You can create custom attribute using Upgrade script also: Create file below path:
namespace Custom\Product\Setup\UpgradeData.php
<?php
namespace Custom\Product\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
class UpgradeData implements UpgradeDataInterface {
public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory) {
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if(version_compare($context->getVersion(), '2.1.51') < 0)
{
$eavSetup->updateAttribute(
\Magento\Catalog\Model\Product::ENTITY, 'custom_attribute_name', [
'type' => 'int',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'frontend' => '',
'label' => 'Custom Attribute Name',
'input' => 'select',
'group' => 'General',
'class' => 'custom_attribute_name',
'source' => 'Custom\Product\Model\YesNo',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false
]
);
}
$setup->endSetup();
}
}
?>
Write custom Source like below:
<?php
namespace Custom\Product\Model;
class YesNo extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
protected $_options;
/**
* getAllOptions
*
* @return array
*/
public function getAllOptions()
{
if ($this->_options === null) {
$this->_options = [
['value' => '1', 'label' => __('Yes')],
['value' => '0', 'label' => __('No')]
];
}
return $this->_options;
}
final public function toOptionArray()
{
return array(
array('value' => '1', 'label' => __('Yes')),
array('value' => '0', 'label' => __('No'))
);
}
}
I hope,this will help you.