17

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');

14

You'll have to use the Magento/Store/Model/Information class and call the getStoreInformationObject() method for that.

Recommended way

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();

Unrecommended way

You should never use the object manager directly (see why here: Magento 2: 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();
  • how to implement it using object manager in phtml – Paras Arora Jul 12 '16 at 12:21
  • @parasarora1303 see my edit but you should never use the object manager directly – Raphael at Digital Pianism Jul 12 '16 at 12:26
  • @RaphaelatDigitalPianism : I'm getting an error Fatal error: Uncaught Error: Call to a member function dispatch() on null in vendor\magento\framework\View\Element\AbstractBlock.php on line 644 --After clearing cache and all.... – Kaushal Suthar Dec 6 '16 at 8:46
  • 2
    You need to pass the store as an argument of the function getStoreInformationObject – Franck Garnier Aug 7 '17 at 12:04
  • 1
    This answer still is not right. $store is not defined. – Cypher909 Aug 15 '18 at 22:05
7
$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();
7

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()

1

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();
1

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();?>

0

We can also use :

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$storePhone = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('general/store_information/phone');

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.