Magento 2 Add Product Attribute Programmatically

Magento2 manages Product by EAV model, So we cannot directly add an attribute for the product by adding a column for the product table. Will check how we can achieve this.

I’m going to add custom_qty as custom product Attribute.

Create UpgradeSchema.php file under the Setup folder. using MageChallenge_SetupData

File path: app/code/MageChallenge/SetupData/Setup/UpgradeSchema.php

<?php
namespace MageChallenge\SetupData\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Eav\Setup\EavSetupFactory;

/**
 * Class UpgradeSchema
 * @package MageChallenge\SetupData\Setup
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        if (version_compare($context->getVersion(), '1.0.2', '<')) {
            $eavSetup = $this->eavSetupFactory->create();
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'custom_qty',
                [
                    'type' => 'text',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Custom Qty',
                    'input' => 'text',
                    'class' => '',
                    'source' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );
        }

        $installer->endSetup();
    }
}

Run php bin/magento setup:upgrade

Go to STORES->Attributes->Product. here you can see custom_qty attribute

Custom product attribute

Now you can add product attribute to the Attribute set

Finally, you can see the product custom attribute from the admin product detail page.

product custom attribute

Thank you. Have a nice day 🙂

Latest posts by Chathura Janaka Liyanage (see all)

11 thoughts on “Magento 2 Add Product Attribute Programmatically

  1. Needed to write you this very small word to help thank you very much yet again with your unique tricks you have shown on this website. This is so wonderfully open-handed with you to supply without restraint what exactly some people would have supplied for an electronic book to make some profit on their own, specifically since you might well have tried it in the event you wanted. Those tips likewise worked to provide a great way to understand that the rest have the same zeal the same as my very own to understand whole lot more with respect to this problem. I think there are thousands of more pleasurable times up front for those who view your website.

  2. I intended to compose you a bit of note in order to give many thanks again about the amazing advice you’ve contributed above. It’s generous of people like you giving easily all a few individuals could possibly have made available as an e book to end up making some cash for themselves, most importantly given that you could possibly have done it if you ever decided. These inspiring ideas also served like the fantastic way to comprehend other individuals have similar eagerness like my own to know great deal more on the subject of this issue. I’m sure there are many more fun occasions in the future for individuals who look over your blog post.

  3. Thanks for your own work on this website. My mum takes pleasure in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very important tips and hints via your website and therefore strongly encourage response from visitors on this theme while our own simple princess is without a doubt understanding so much. Take pleasure in the rest of the year. Your performing a useful job.

  4. I needed to draft you one little observation so as to say thank you as before for these great knowledge you’ve documented here. It has been certainly pretty generous of people like you to convey freely exactly what a number of people could possibly have made available as an e book to earn some cash for their own end, even more so considering the fact that you could possibly have done it if you ever decided. The ideas also acted like a fantastic way to comprehend other individuals have similar dreams like my own to grasp good deal more in terms of this issue. I’m sure there are many more fun occasions ahead for individuals that go through your blog post.

  5. I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let other folks smoothly know just exactly several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie.

  6. Thank you for your entire labor on this web site. My mom really loves managing investigations and it is obvious why. My partner and i learn all regarding the lively tactic you produce sensible things through your web site and in addition invigorate response from the others on the situation then our favorite princess is truly starting to learn a whole lot. Take advantage of the rest of the year. You have been doing a stunning job.

  7. I wanted to post you that tiny remark to say thanks a lot over again just for the nice secrets you have featured in this case. It was really seriously open-handed with you to grant publicly just what many individuals might have offered for an ebook to generate some dough for themselves, mostly now that you might have tried it in case you desired. These secrets as well served to be a good way to fully grasp that other people online have the identical fervor much like my personal own to know the truth many more regarding this matter. I am sure there are numerous more pleasant periods in the future for many who looked over your site.

Leave a Reply