0

I need following if in admin i click on button i need to write some system values based on the current scope.

/** @var \Magento\Config\Model\ResourceModel\Config $resourceConfig */
$resourceConfig->saveConfig('path/to/field', $value, $scope, $scopeId);

so the question is how to get the scope?

0

1 Answer 1

0

fixed issue by adding some parameters in to javascript call, basicaly we check if in admin url we have either store or website parameter and passing it to the script.

var a = window.location.pathname.split('/');
if(a.indexOf('website') !== -1){
    a.splice(0, a.indexOf('website'));
    params = {
        'website': a[1]
    };
} else {
    if(a.indexOf('store') !== -1){
        a.splice(0, a.indexOf('store'));
        params = {
            'store': a[1]
        };
    } else {
        params = {
        };
    }
}
new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
    loaderArea:     false,
    asynchronous:   true,
    parameters:     params,
    onSuccess: function(transport) {
         var response = JSON.parse(transport.responseText);
    }
});

also rewrited part of the save function, it seem the first method didn't save the config value directly but only after you click save manually.

\Magento\Framework\App\RequestInterface $request,
\Magento\Config\Model\Config\Factory $configFactory
...
$this->_configFactory = $configFactory;
$this->_request = $request;
...
$configData = [
    'section' => 'path',
    'website' => ($this->_request->getParam('website', 0) ? $this->_request->getParam('website', 0):null),
    'store'   => ($this->_request->getParam('store', 0) ? $this->_request->getParam('store', 0) : null),
    'groups'  => [
        'to' => [
            'fields' => [
                'field' => [
                    'value' => $value,
                ],
            ],
        ],
    ],
];
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->_configFactory->create(['data' => $configData]);
$configModel->save();

now this saves value based on website / store view

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.