0

I tried using this function

$this->_scopeConfig->getValue(
      'general/store_information/phone',
      \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES
    );

And it is always returning an null value. This is on all general store settings. Is there a way to retrive store website phone number? Use case display store phone number in checkout.

1

2 Answers 2

2

The store config values STORES > Configuration are stored in core_config_data table

enter image description here

There are 5 columns, however, we should focus on 4 columns:

+scope: The scope to use to determine config value, e.g., store or default. We have some scopes:

__Magento\Framework\App\Config::SCOPE_TYPE_DEFAULT

__vendor/magento/module-store/Model/ScopeInterface.php

interface ScopeInterface
{
    /**#@+
     * Scope types
     */
    const SCOPE_STORES = 'stores';

    const SCOPE_WEBSITES = 'websites';

    const SCOPE_STORE   = 'store';
    const SCOPE_GROUP   = 'group';
    const SCOPE_WEBSITE = 'website';
    /**#@-*/
}

+scope_id they are store ids. We can find these values in store table.

+path: The path through the tree of configuration values, e.g., general/store_information/name. When developing a module, we declare these values in etc/adminhtml/system.xml

+value: config values.

So, when we want to get the config values, we need to know scope, scope_id and path. Magento had a method to retrieve config value by path, scope and scope code (store id):

vendor/magento/framework/App/Config.php

vendor/magento/framework/App/Config/ScopeConfigInterface.php

public function getValue(
    $path, 
    $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 
    $scopeCode = null
);

In your case, you want to get the phone value with websites scope and path general/store_information/phone. We should start to debug with a raw sql to check the available phone numbers:

SELECT * FROM core_config_data a WHERE a.`path` LIKE '%general/store_information/phone%'

enter image description here

After executing this query, we make ensure that the phone number is available or not in our scope: \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES. If not available in our scope, we should set in admin config.

SCOPE_WEBSITES correlates with scope id (store id), so we need to set scope id (store id) for getValue() method. For example:

    //Getting current store id
    /**
     * @var \Magento\Store\Model\StoreManagerInterface $storeManager
    */
    ......
    $scopeCode = $this->storeManager->getStore()->getStoreId()

    $this->_scopeConfig->getValue(
      'general/store_information/phone',
      \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
     $scopeCode
    );
2

Inject ScopeConfigInterface and StoreManagerInterface following way:

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
protected $scopeConfig;


public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    $this->storeManager = $storeManager;
    $this->scopeConfig = $scopeConfig;
}

Now write following line for output:

$this->scopeConfig->getValue('general/store_information/phone', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->storeManager->getStore()->getStoreId());

Extra info:

For future reference: This works but if you are extending \Magento\Framework\View\Element\Template don't forget to make the constructor like the parent class.

public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, array $data = [] ) { parent::__construct($context, $data); $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; }

And if you accessing it in phtml make the variables public instead of private.

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.