Magento 2: Add Product Attribute Using Data Patch

Magento 2 create custom product attribute using data patch adobe commerce

Let’s Create a custom product attribute using a data patch. Install Data & Upgrade Data is replaced by the Data Patch since Magento 2.3 version.

Install Data & Upgrade Data are depreciated so Magento 2 recommends to use Data Patch to create EAV attributes.

Create custom product attributes using the data patch.

Let’s understand how we can create custom product attributes using the data patch on this blog. Create a basic module structure before creating a data patch, then follow the steps given below.

1) Define Data Patch Class

You need to defined a data patch class

Vendor_Module/Setup/Patch/Data/ directory.

<?php
/**
 * @copyright Copyright (c) sbdevblog (https://www.sbdevblog.com)
 */

declare (strict_types = 1);
namespace SbDevBlog\Catalog\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
/**
 * Class Create AdditionalProductInfo Attribute
 */
class AdditionalProductInfo implements DataPatchInterface {
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory          $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * @inheritdoc
     */
    public function apply() {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute('catalog_product', 'additional_product_info', [
            'type' => 'text',
            'label' => 'Additional Product Information',
            'input' => 'text',
            'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => '',
        ]);
    }
    /**
     * @inheritdoc
     */
    public static function getDependencies() {
        return [];
    }
    /**
     * @inheritdoc
     */
    public function getAliases() {
        return [];
    }
}

Run php bin/magento setup:upgrade in order to apply patch to create an attribute.

Data patches are really simple and easy to use. Please visit https://sbdevblog.com/magento2-how-to-add-customer-address-attribute-using-data-patch/ to create the customer attributes using the data patch.

You may visit my git repo https://github.com/sbdevblog/catalog

sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *