Magento 2 create product attributes using install scripts


  1. Under your Magento 2 module, create a directory called “Setup”. (Create Magento 2 Module)
  2. Create a new file: “InstallData.php” inside the Setup directory with the below content.

    
    <?php
    namespace Magleaks\Catalog\Setup;
    
    use Magento\Catalog\Model\Product;
    use Magento\Catalog\Model\Product\Type;
    use Magento\Catalog\Model\Resource\Eav\Attribute;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    
    /**
     * Class InstallData
     *
     * @package Magleaks\Catalog\Setup
     */
    class InstallData implements InstallDataInterface
    {
        /**
         * @var \Magento\Eav\Setup\EavSetupFactory
         */
        private $eavSetupFactory;
        /**
         * @param EavSetupFactory $eavSetupFactory
         */
        public function __construct(EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }
        /**
         * @param ModuleDataSetupInterface $setup
         * @param ModuleContextInterface   $context
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $productTypes = [
                Type::TYPE_SIMPLE,
                Type::TYPE_VIRTUAL,
            ];
            $productTypes = join(',', $productTypes);
            $eavSetup->addAttribute(
                Product::ENTITY,
                'on_sale',
                [
                    'type'                    => 'int',
                    'label'                   => 'Sale',
                    'input'                   => 'boolean',
                    'sort_order'              => 50,
                    'global'                  => Attribute::SCOPE_WEBSITE,
                    'user_defined'            => true,
                    'required'                => false,
                    'used_in_product_listing' => true,
                    'apply_to'                => $productTypes,
                    'group'                   => 'General',
                    'unique'                  => false,
                    'visible_on_front'        => false,
                    'searchable'              => false,
                    'filterable'              => false,
                    'comparable'              => false,
                    'visible'                 => true,
                    'backend'                 => '',
                    'frontend'                => '',
                    'class'                   => '',
                    'source'                  => '',
                    'default'                 => '',
                ]
            );
        }
    }
    
    
  3. Go to your Magento 2 root folder from your terminal and run

    
    bin/magento setup:upgrade
    

Leave a Reply

  • (will not be published)