Reading list Switch to dark mode

    How to save system configuration field value programmatically Magento2

    Updated 6 July 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!!

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home