1

I created attribute in Magento 2 using install script. By this code.

namespace Custom\Features\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;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
   private $eavSetupFactory;
   private $attributeSetFactory;
   private $attributeSet;
   private $categorySetupFactory;

   public function __construct(EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory )
       {
           $this->eavSetupFactory = $eavSetupFactory;
           $this->attributeSetFactory = $attributeSetFactory;
           $this->categorySetupFactory = $categorySetupFactory;
       }

   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
   {    // TO CREATE PRODUCT ATTRIBUTE
       if (version_compare($context->getVersion(), '1.0.6') < 0) {
       $setup->startSetup();
       $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
           $eavSetup->addAttribute(
                   \Magento\Catalog\Model\Product::ENTITY,
               'feature_1',
               [
                    'group' => 'Autosettings',
                    'type'  => 'varchar',
                    'label' => 'Feature 1',
                    'input' => 'text',
                    'frontend' => '',
                    'required' => false,
                    'global' => 1,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => true,
                    'default' => null,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => true,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => '',
                    'note' => 'Feature 1'
           ]
       );  

       $setup->endSetup();
   }
   }
} 

Now I want create some attribute in catalog using Upgrade.

How to process same with another attribute in catalog using upgrade script.

2 Answers 2

1

First You need to increase version of your file under etc/module.xml.Suppose your version is 1.0.0 then increased it by 1.0.1

Now create UpgradeData.php under Setup folder.

    <?php 
    namespace WilliamsCommerce\OrderBy\Setup;
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Eav\Setup\EavSetup;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Catalog\Setup\CategorySetupFactory;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

    class UpgradeData implements UpgradeDataInterface
    {
       private $eavSetupFactory;
       private $attributeSetFactory;
       private $attributeSet;
       private $categorySetupFactory;

       public function __construct(EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory )
           {
               $this->eavSetupFactory = $eavSetupFactory;
               $this->attributeSetFactory = $attributeSetFactory;
               $this->categorySetupFactory = $categorySetupFactory;
           }

       public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
       {    // TO CREATE PRODUCT ATTRIBUTE
           if (version_compare($context->getVersion(), '1.0.1') < 0) {
           $setup->startSetup();
           $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
               $eavSetup->addAttribute(
                       \Magento\Catalog\Model\Product::ENTITY,
                   'feature_1',
                   [
                        'group' => 'Product Details',
                        'type'  => 'varchar',
                        'label' => 'Feature 1',
                        'input' => 'text',
                        'frontend' => '',
                        'required' => false,
                        'global' => 1,
                        'visible' => true,
                        'required' => false,
                        'user_defined' => true,
                        'default' => null,
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => true,
                        'used_in_product_listing' => true,
                        'unique' => false,
                        'apply_to' => '',
                        'note' => 'Feature 1'
               ]
           );  

           $setup->endSetup();
       }
       }
    } 
?>
3
  • Eav attribute not custom table.. Commented Aug 16, 2018 at 6:42
  • OK i am updating my code i have just created using upgrade data. Commented Aug 16, 2018 at 7:01
  • If it is use full to you then accept also it will help to others to find relevant answer. Thanks. Commented Aug 16, 2018 at 7:50
0

Its the same think you need to create an upgradData

https://devdocs.magento.com/guides/v2.2/ui_comp_guide/howto/add_category_attribute.html

Tell me if you need more help, i can post you a code if you need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.