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:
<?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.
Do share the post with fellow Magento developers via social media.
Thank you.