How To Set Store Configuration Value In InstallSchema In Magento 2
The post gives the solution to programmatically set store configuration value in InstallSchema in Magento 2.
For example, you are using a Magento 2 extension in your store and want to change the default Magento 2 field values while extension installation. The below code is the solution for the same.
Changing the default field values in Magento 2 while extension installation becomes easy with this method. You can also implement this manually from the admin panel by logging in and navigate to Stores > Configuration.
Method to Set Store Configuration Value In InstallSchema in Magento 2:
Place the below code in app/code/Vendor/Extension/InstallSchema.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace Vendor\Extension\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\App\Config\ConfigResource\ConfigInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\Store; class UpgradeSchema implements UpgradeSchemaInterface { protected $resourceConfig; public function __construct( ConfigInterface $resourceConfig) { $this->resourceConfig = $resourceConfig; } public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $this->resourceConfig->saveConfig( 'checkout/cart/delete_quote_after', '3000', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, Store::DEFAULT_STORE_ID ); $setup->endSetup(); } } |
That’s it.
Any doubts? Please feel free to mention them in the Comments section below. I’d be happy to help you out.
Do share the post with fellow Magento developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Call phtml File In Another phtml File In Magento 2
Coronavirus – Causes, Symptoms, & Precautions
Next