I want to display phone number saved in magento admin in frontend in magento 2.
Like in magento 1.9 its like
$storePhone = Mage::getStoreConfig('general/store_information/phone');
You'll have to use the Magento/Store/Model/Information
class and call the getStoreInformationObject()
method for that.
You'll have to inject this class in your custom block to be able to use that in your template though.
protected $_storeInfo;
public function __construct(
....
\Magento\Store\Model\Information $storeInfo,
....
) {
...
$this->_storeInfo = $storeInfo;
....
}
Then create a custom method to retrieve the phone number:
public function getPhoneNumber()
{
return $this->_storeInfo->getStoreInformationObject(Store $store)->getPhone();
}
Thus in your template you can call:
$block->getPhoneNumber();
You should never use the object manager directly (see why here: To use or not to use the ObjectManager directly?)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('\Magento\Store\Model\Information');
$storeInfo = $storeInformation->getStoreInformationObject($store);
Then you can get the phone by calling:
$phone = $storeInfo->getPhone();
you need to inject the an instance of \Magento\Framework\App\Config\ScopeConfigInterface
in your block.
protected $scopeConfig;
public function __construct(
....
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
....
) {
...
$this->scopeConfig = $scopeConfig;
....
}
Then create the method getStorePhone()
public function getStorePhone()
{
return $this->scopeConfig->getValue(
'general/store_information/phone',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
and call in your template echo $block->getStorePhone()
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento\Store\Model\Information');
$store = $objectManager->create('Magento\Store\Model\Store');
$storeInfo = $storeInformation->getStoreInformationObject($store);
$phone = $storeInfo->getPhone();
Above methods was not working for so I've tried in following way and it's working for me...
namespace Vendor\Module\Block;
class Contact extends \Magento\Framework\View\Element\Template
{
protected $_storeInfo;
protected $_storeManagerInterface;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Store\Model\Information $storeInfo,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
array $data = []
)
{
parent::__construct($context, $data);
$this->_storeInfo = $storeInfo;
$this->_storeManagerInterface = $storeManagerInterface;
}
public function getPhoneNumber()
{
return $this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore(null))->getPhone();
}
}
and in template file I've called
echo $block->getPhoneNumber();
The above code is not working for me . I have tried the following code that works.
class Sociallinks extends \Magento\Framework\View\Element\Template
{
protected $socialLinksHelper;
protected $objMgr;
protected $storeInfo;
protected $scopeConfig;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Addpeople\Websettings\Helper\Data $myModuleHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->_socialLinksHelper = $myModuleHelper;
$this->_objMgr = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $this->_objMgr->create('Magento\Store\Model\Information');
$store = $this->_objMgr->create('Magento\Store\Model\Store');
$this->_storeInfo = $storeInformation->getStoreInformationObject($store);
}
public function getPhoneNumber()
{
return $this->_storeInfo->getPhone();
}
}
Template file
<?php echo $block->getPhoneNumber();?>
We can also use :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storePhone = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('general/store_information/phone');