1

In Magento 1 it was possible to set/retrieve config values by website or store i.e

by website:

Mage::app()->getWebsite()->getConfig('settings/reference/prefix');

or via store

 Mage::app()->getStore()->getConfig('settings/reference/prefix');

However in magento 2 the config is set via the

Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

$this->_scopeConfig->getValue('settings/reference/prefix') 

My question now is, how can I retrieve the values by website/store?

2

Here's how i do it in my helper:

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    protected $storeManager;
    protected $objectManager;

    const XML_PATH_KEY = 'vendor_module/general/key'; 


    public function __construct(Context $context,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager
    ) {
        $this->objectManager = $objectManager;
        $this->storeManager  = $storeManager;
        parent::__construct($context);
    }

    public function getConfigValue($field, $storeId = null)
    {
        return $this->scopeConfig->getValue(
            $field, ScopeInterface::SCOPE_STORE, $storeId
        );
    }

    public function getApiKey($storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_KEY, $storeId);
    }

}

you can change the SCOPE_STORE to following:

const SCOPE_STORES   = 'stores';
const SCOPE_GROUPS   = 'groups';
const SCOPE_WEBSITES = 'websites';

const SCOPE_GROUP   = 'group';
const SCOPE_WEBSITE = 'website';
| improve this answer | |
3

Just Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, In your constructor argument and set the class property: $this->scopeConfig = $scopeConfig;

Now to Get the configuration value for Store or Website View use:

Store View:

$this->scopeConfig->getValue('settings/reference/prefix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

Website View:

$this->scopeConfig->getValue('settings/reference/prefix', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
| improve this answer | |
0
$this->_scopeConfig->getValue('settings/reference/prefix', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); //Get value by Website

$this->_scopeConfig->getValue('settings/reference/prefix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); //Get value by store
| improve this answer | |
0

In template (phtml) files, I use in the following way:

$config = $block->getLayout()->createBlock(\Magento\Config\Block\System\Config\Form::class);
$configValue = $config->getConfigValue('web/secure/base_url'); // website, store will be detected automatically
| improve this answer | |

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.