1

i want to update the store configuration value using Data Patch.

Note: please do not suggest install and upgrade script.

i tried below code but its not working:

   <?php
 /**
  * Copyright ©  All rights reserved.
  * See COPYING.txt for license details.
  */
declare(strict_types=1);

namespace Vendor\Modulename\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_TELEPHONE = 'customer/address/telephone_show';

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_TELEPHONE, 'opt', self::SCOPE_STORE);
    $this->moduleDataSetup->endSetup();
}

public function getAliases()
{
    return [];
}

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

1 Answer 1

2

Please use this : -

<?php

namespace Vendor\Modulename\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class SetupConfigData implements DataPatchInterface
{
    private $moduleDataSetup;
    private $configWriter;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->configWriter = $configWriter;
    }

    public function apply()
    {
        $this->moduleDataSetup->startSetup();
        
        $this->configWriter->save(
            'customer/address/telephone_show',
            1,
            \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
            0
        );

        $this->moduleDataSetup->endSetup();
    }

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

    public function getAliases()
    {
        return [];
    }
}

Your Answer

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

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