Productattribute

Install Magento 2 product attributes programmatically.

The attribute is automatically added to all the attribute sets.

Use the Productattribute snippet in the Magento 2 module creator.

Files

Setup/ProductSetup.php

<?php


namespace Mage2Gen\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Catalog\Setup\CategorySetup;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Model\ResourceModel\Product;

class ProductSetup extends EavSetup
{

    public function getDefaultEntities()
    {
        return [
            'catalog_product' => [
                'entity_type_id' => CategorySetup::CATALOG_PRODUCT_ENTITY_TYPE_ID,
                'entity_model' => Product::class,
                'attribute_model' => Attribute::class,
                'table' => 'catalog_product_entity',
                'additional_attribute_table' => 'catalog_eav_attribute',
                'entity_attribute_collection' =>
                Collection::class,
                'attributes' => [

                    'test' => [
                            'type' => 'varchar',
                            'label' => 'Test',
                            'input' => 'text',
                            'source' => '',
                            'frontend' => '',
                            'required' => false,
                            'note' => '',
                            'class' => '',
                            'backend' => '',
                            'sort_order' => '30',
                            'global' => 1,
                            'default' => null,
                            'visible' => true,
                            'user_defined' => true,
                            'searchable' => false,
                            'filterable' => false,
                            'comparable' => false,
                            'visible_on_front' => false,
                            'unique' => false,
                            'apply_to' => '',
                            'group' => 'General',
                            'used_in_product_listing' => false,
                            'is_used_in_grid' => true,
                            'is_visible_in_grid' => false,
                            'is_filterable_in_grid' => false,
                            'option' => array('values' => array(""))
                    ],

        
                ]
            ]
        ];
    }
}

Setup/Patch/Data/InstallProductAttributes.php

<?php


namespace Mage2Gen\Module\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Mage2Gen\Module\Setup\ProductSetupFactory;

class InstallProductAttributes implements DataPatchInterface
{

    private $moduleDataSetup;
    private $productSetupFactory;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param ProductSetupFactory $productSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        ProductSetupFactory $productSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->productSetupFactory = $productSetupFactory;
    }

    /**
     * Do Upgrade
     *
     * @return void
     */
    public function apply()
    {
        /** @var ProductSetup $productSetup */
        $productSetup = $this->productSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $productSetup->installEntities();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [
        
        ];
    }
}