Select Page

How to save system configuration field value programmatically Magento2

Radhika Garg
Published: July 6, 2022

In this guide, we will learn how we can set system configuration field value programmatically.
The following interface can used to achieve this result using it’s save method:
/vendor/magento/framework/App/Config/Storage/WriterInterface.php

<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Test
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Test\Controller\Adminhtml\Index;
use Magento\Framework\App\Config\ScopeConfigInterface;
class SaveValue
{
    
    /**
     * @var Magento\Framework\App\Config\Storage\WriterInterface 
     */
    protected $configWriter;
    /**
     * Construct
     *
     * @param Context $context
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
     */
    public function __construct(
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
    ) {
        
        $this->configWriter = $configWriter;
    }
    public function setValue($path, $value)
    {
        $this->configWriter->save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
           
    }
}

We hope it will help you. Thank you!!

If any issue or doubt please feel free to mention in comment section.

We would be happy to help. Happy Coding!!

Source: webkul.com