2

How I should 'Setup/update System/store Configuration values through setup script'? Trying with Setup/Patch/Data script using something like

//app/code/Namespace/Module/Setup/Patch/Data/SetupConfigData.php

namespace Namespace\Module\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\App\Config\MutableScopeConfigInterface;

class SetupConfigData implements DataPatchInterface
{

const XML_PATH_STORE_NAME = 'general/store_information/name';

const XML_PATH_STORE_PHONE = 'general/store_information/phone';

const XML_PATH_ADMIN_ACCOUNT_SHARING = 'admin/security/admin_account_sharing';

const XML_PATH_ADMIN_URL_SECRET_KEY = 'admin/security/use_form_key';

const SCOPE_STORE = 'store';

/**
 * @var \Magento\Framework\App\Config\MutableScopeConfigInterface
 */
private $mutableScopeConfig;

/**
 * @var ModuleDataSetupInterface
 */
private $moduleDataSetup;

public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    MutableScopeConfigInterface $mutableScopeConfig
)
{

    $this->moduleDataSetup = $moduleDataSetup;
    $this->mutableScopeConfig = $mutableScopeConfig;
}

public function apply()
{
    $this->moduleDataSetup->startSetup();
    $this->mutableScopeConfig->setValue(self::XML_PATH_STORE_NAME, "Title", self::SCOPE_STORE);
    $this->mutableScopeConfig->setValue(self::XML_PATH_STORE_PHONE, "Default contact", self::SCOPE_STORE);
    $this->mutableScopeConfig->setValue(self::XML_PATH_ADMIN_ACCOUNT_SHARING, 0, self::SCOPE_STORE);
    $this->mutableScopeConfig->setValue(self::XML_PATH_ADMIN_URL_SECRET_KEY, 0, self::SCOPE_STORE);
    $this->moduleDataSetup->endSetup();
}

public function getAliases()
{
    return [];
}

public static function getDependencies()
{
    return [];
}
}

Environment: Magento 2.3.3 and nothing changed after running setup:upgrade, am not sure what am I missing.

2 Answers 2

2

Have you looked at answers to questions like this

https://magento.stackexchange.com/a/104379/70343

This seems to work:

class InstallData implements InstallDataInterface 
{
    public function __construct(
        LoggerInterface $loggerInterface,
        \Magento\Framework\App\Config\ConfigResource\ConfigInterface  $resourceConfig)
    {
        $this->logger = $loggerInterface;
        $this->resourceConfig = $resourceConfig;
    }        

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->resourceConfig->saveConfig(
            'a/b/c', 
            'value', 
            \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 
            \Magento\Store\Model\Store::DEFAULT_STORE_ID
        );

        $setup->endSetup();
    }   
}
0

for System Config, it is better to use the config.xml. Maybe you have already applied this change to Database this information you can find with the following Query. A Data Patch applies only one time on a System.

Select * FROM patch_list WHERE patch_name
LIKE '%SetupConfigData%'

best regards,

Lars

1
  • the fields that I want to update are Magento core system config settings, using config.xml didnt worked so tried Patch/Data and that is not working as expected
    – Elamurugan
    Dec 18, 2019 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.