---
title: "Magento 2: How to Save Configuration Automatically when Extension is Installed"
url: "https://magecomp.com/blog/magento-2-save-configuration-automatically-when-extension-installed/"
date: "2024-09-30T16:41:35+00:00"
modified: "2025-02-12T09:16:22+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 347
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Learn how to automatically save configuration settings during Magento 2 extension installation."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "The Top-Rated Facebook Shop Integration Extension with Magento 2"
    url: "https://magecomp.com/blog/best-facebook-shop-integration-extension-for-magento-2/"
  - title: "How to Add Date &amp; Time Picker in Magento System Configuration?"
    url: "https://magecomp.com/blog/add-date-time-picker-in-magento-system-configuration/"
  - title: "How to Set Product Image using Image URL Link Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/set-product-image-using-image-url-link-programmatically-magento-2/"
---

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

_Published: September 30, 2024_  
_Author: Dhiren Vasoya_  

![Magento 2 How to Save Configuration Automatically when Extension is Installed](https://magecomp.com/blog/wp-content/uploads/2024/09/Magento-2-How-to-Save-Configuration-Automatically-when-Extension-is-Installed-1024x512.webp)

**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.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-4.webp)](https://magecomp.com/services/hyva-theme-development/)

In this blog post, we will walk through the steps required to save configuration settings automatically when [**installing an extension in Magento 2**](https://magecomp.com/magento-professional-installation.html). 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 VendorExtensionSetupPatchData;

use MagentoFrameworkAppResourceConnection;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkAppConfigStorageWriterInterface;
use MagentoFrameworkModuleDir;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;

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.
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

## 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**](https://magecomp.com/magento-2-extensions.html).
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-save-configuration-automatically-when-extension-installed/](https://magecomp.com/blog/magento-2-save-configuration-automatically-when-extension-installed/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-17 03:01:53 UTC_  
