Is there a way to do some action at module registration/installation?
It can be through event observer or through migration.
I want to call a function to get some values and store them in core_config_data during installation.
Thanks.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou can use InstallData.php to store value in core_config_data during installation
use below code for example :
$configdata = [
'scope' => DEFINE_YOUR_SCOPE
'scope_id' => 0,
'path' => DEFINE_YOUR_PATH
'value' => YOUR_VALUE,
];
$setup->getConnection()->insertOnDuplicate($setup->getTable('core_config_data'), $configdata, ['value']);
Try to save config values via config.xml
First Method:
app/code/VendorName/ModuleName/etc/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<sectionName>
<groupName>
<fieldName>add value here</fieldName>
</groupName>
</sectionName>
</default>
</config>
For eg.
<default>
<payment>
<checkmo>
<active>1</active>
<order_status>pending</order_status>
<title>Check / Money order</title>
</checkmo>
</payment>
</default>
Second Method:
<?php
namespace VendorName\ModuleName\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class InstallData implements InstallDataInterface
{
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$data = [
'scope' => 'default',
'scope_id' => 0,
'path' => "{section-name}/{group-name}/{field-name}",
'value' => "Value",
];
$setup->getConnection()
->insertOnDuplicate($setup->getTable('core_config_data'), $data, ['value']);
}
}
After run below commands: