Categories: How ToMagento 2

How to Run Extension Setup Script from Magento 2 Root

If you are developing custom Magento extensions, you might have database script which require to be properly run when you install the extension. Sometimes while installation, it may happen where the extension is installed but database script is missing. Here the setup scripts come in place where you need to run extension setup script from Magento 2 root to properly install the extension.

Let’s have a look at setup script to run extension from Magento 2 root.

Here, as a sample code, I’m showing to create a category and product attribute through setup script.

getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try
{
 $setup = $obj->get('Magento\Framework\Setup\ModuleDataSetupInterface');
 $eav_setup_factory = $obj->get('Magento\Eav\Setup\EavSetupFactory');
 
  $setup->startSetup();
    $eavSetup = $eav_setup_factory->create(['setup' => $setup]);
  // Product Attribute
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'customattribute',
            [
                'type' => 'text',
                'label' => 'Custom Attribute Label',
    'backend' => '',
                'input' => 'textarea',
    'wysiwyg_enabled'   => true,
    'source' => '',
                'required' => false,
                'sort_order' => 3,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'group' => 'custom attribute group',
                'used_in_product_listing' => true,
                'visible_on_front' => true
            ]
        );
  
  //Category Attribute Create Script
  $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            ' customattribute ',
   [
     'group' => 'custom attribute group',
     'label' => 'custom attribute label',
                 'type' => 'text',
                 'input' => 'textarea',
     'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
     'required' => false,
                 'sort_order' => 25,
     'source' => '',
                 'is_used_in_grid' => true,
                 'is_visible_in_grid' => false,
                 'is_filterable_in_grid' => true,
     'wysiwyg_enabled' => true
            ]
        );
        $setup->endSetup();
echo "Code Executed Successfully";
}
catch(\Exception $e)
{
 echo $e->getMessage();
 exit;
}

Running above setup script from Magento 2 root will instantly install the extension with related missing database. Hope this blog has helped you to properly install extension. If you get confused or have questions regarding, feel free to comment, I’ll be happy to help you.

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago