{"id":541,"date":"2019-08-07T16:43:40","date_gmt":"2019-08-07T16:43:40","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/08\/07\/programmatically-set-magento-2-core-config-data\/"},"modified":"2025-05-22T16:12:05","modified_gmt":"2025-05-22T10:42:05","slug":"programmatically-set-magento-2-core-config-data","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-set-magento-2-core-config-data\/","title":{"rendered":"How to Programmatically Set Magento 2 Core Config Data"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ever had to programmatically set Magento 2 core config data? Not only that but afterward, retrieve that new value?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I had to! And, today, I\u2019ll share the method to set core config data programmatically in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have also added how I managed the enabled cache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following interface can be used to achieve that<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\/vendor\/magento\/framework\/App\/Config\/Storage\/WriterInterface.php<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">using the following <strong>save<\/strong>&nbsp;method<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>public function save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, to delete a core config data value you can use the <strong>delete<\/strong>&nbsp;method<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>public function delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Execute the below code for the solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mehod to programmatically set Magento 2 Core Config Data:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor_Name\\Extension_Name\\Helper;\nuse Magento\\Framework\\App\\Config\\Storage\\WriterInterface;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nclass Data\n{\n   protected $configWriter;\n   public function __construct(WriterInterface $configWriter) {\n       $this->configWriter = $configWriter;\n   }\n   \/**\n    * @param $path = 'extension_name\/general\/data'\n    * @param $value = '1'\n    *\/\n   public function SetData($path, $value) {\n       $this->configWriter->save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever had to programmatically set Magento 2 core config data? Not only that but afterward, retrieve that new value? I had to! And, today, I\u2019ll&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-541","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=541"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/541\/revisions"}],"predecessor-version":[{"id":15319,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/541\/revisions\/15319"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}