2

I know how to change single config value programmatically, by this way

$configData = [
    'section' => 'MY_SECTION',
    'website' => null,
    'store'   => null,
    'groups'  => [
        'MY_GROUP' => [
            'fields' => [
                'MY_FIELD' => [
                    'value' => $myValue,
                ],
            ],
        ],
    ],
];

// $this->configFactory --> \Magento\Config\Model\Config\Factory
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
$configModel->save();

But now I want to know, can we set multiple config field values by this way or any other way ?

2
1

We can use the Magento Database Adapter to insert or update the data.

<?php

namespace Vendor\Config\Helper;

class Data
{
    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $adapter;

    /**
     * Config Helper data constructor.
     * @param \Magento\Framework\App\ResourceConnection $adapter
     */
    public function __construct(
        \Magento\Framework\App\ResourceConnection $adapter
    )
    {
        $this->adapter = $adapter;
    }

    public function checkDataConfig()
    {
        $connection = $this->getConnection();
        $table = $connection->getTableName('core_config_data');

        $select = $connection->select()->from(
            $table,
            ['config_id', 'value']
        )->where(
            'path = ?',
            'checkout/options/onepage_checkout_disabled'
        );

        $data = $connection->fetchAll($select);

        if ($data) {
            try {
                $connection->beginTransaction();

                foreach ($data as $value) {
                    $bind = ['path' => 'checkout/options/onepage_checkout_enabled', 'value' => !(bool)$value['value']];
                    $where = 'config_id = ' . $value['config_id'];
                    $connection->update($table, $bind, $where);
                }

                $connection->commit();
            } catch (\Exception $e) {
                $connection->rollback();
                throw $e;
            }
        }

    }

    /**
     * Get connection
     *
     * @return \Magento\Framework\DB\Adapter\AdapterInterface
     */
    public function getConnection()
    {
        return $this->adapter->getConnection();
    }
}

I follow the same logic of checkout module -vendor/magento/module-checkout/Setup/InstallData.php.

[EDIT]

We also try with Magento\Config\Model\Config::setDataByPath() method.

3
  • Are you sure table name should be "core_config_data" ? – Mrunmay Deswandikar Apr 11 '17 at 9:40
  • 1
    Yes, I think so. We also try with Magento\Config\Model\Config::setDataByPath() method. See my updated answer. – Khoa TruongDinh Apr 11 '17 at 9:46
  • Yeah! Hope it's useful for you. – Khoa TruongDinh Apr 11 '17 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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