19

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

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
Paras Arora
  • 839
  • 2
  • 11
  • 29

6 Answers6

15

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: 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();
Niraj Patel
  • 907
  • 3
  • 37
  • 79
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
11

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

Mujahidh
  • 2,793
  • 4
  • 37
  • 82
Manashvi Birla
  • 8,833
  • 9
  • 27
  • 53
8
$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();
Marius
  • 197,939
  • 53
  • 422
  • 830
Alex
  • 81
  • 1
  • 1
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();
Kaushal Suthar
  • 3,227
  • 5
  • 31
  • 57
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();?>

Rahul Shukla
  • 365
  • 3
  • 6
Liz Eipe C
  • 1,286
  • 12
  • 17
0

We can also use :

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