How To

Magento 2: How to Save Configuration Automatically when Extension is Installed

Hello Magento Friends,

Magento 2 is a powerful and flexible eCommerce platform, known for its vast extensibility. When creating or installing a new extension, one key requirement is saving default configuration settings automatically, without requiring manual input from the user. This approach improves the user experience and streamlines the installation process.

In this blog post, we will walk through the steps required to save configuration settings automatically when installing an extension in Magento 2. This feature is helpful when you want to ensure that your extension works out of the box with predefined settings.

Steps to Save Configuration Automatically when Extension is Installed in Magento 2:

Step 1: Create a “Saveconfiguration.php” file inside the Extension setup folder.

app\code\Vendor\Extension\Setup\Patch\Data

Then add the code as follows

<?php
namespace Vendor\Extension\Setup\Patch\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Module\Dir;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Saveconfiguration implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */    private $moduleDataSetup;

    /**
     * @var WriterInterface
     */    protected $configWriter;

    /**
     * @var ScopeConfigInterface
     */    protected $scopeConfig;

    /**
     * @var Dir
     */    protected $dir;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        WriterInterface $configWriter,
        ScopeConfigInterface $scopeConfig,
        Dir $dir
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->configWriter = $configWriter;
        $this->scopeConfig = $scopeConfig;
        $this->dir = $dir;
    }

    /**
     * {@inheritdoc}
     */    public function apply()
    {
     /* Set your configuration here */        $this->configWriter->save('section_id/group_id/field_id',0);
    }

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

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

}

Note: Replace section_id, Group_id and Field_id with your configuration values.

Conclusion:

Automatically saving configuration values when installing a Magento 2 extension is a small yet significant step in improving the user experience. By using data patches to programmatically insert configuration settings into the database, you can ensure that your extension is ready to go immediately after installation, with predefined values already in place.

This method provides a seamless experience for Magento store owners and ensures that your extension runs as intended from the start. Implementing this functionality enhances both the reliability and usability of your Magento 2 extensions.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
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

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

5 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago