Magento 2 – How to Get Store Contact Number and Email Address

magento2

I want to display my magento 2 store contact detail & mail address in the frontend. How to get store contact and email address in magento 2 and display it in header.phtml file?

Best Answer

The following code worked for me.

<?php

namespace Addpeople\Websettings\Block;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Footerlink extends \Magento\Framework\View\Element\Template
{
// protected $flHelper;
protected $_urlInterface;
protected $scopeConfig;


public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\UrlInterface $urlInterface,
    ScopeConfigInterface $scopeConfig,
    // \Addpeople\Websettings\Helper\Footerlink $myModuleHelper,
    array $data = []
) {
    parent::__construct($context, $data);
    // $this->_flHelper = $myModuleHelper;
    $this->_urlInterface = $urlInterface;
    $this->scopeConfig = $scopeConfig;

}
public function getPhoneNumber()
{

    return $this->scopeConfig->getValue('general/store_information/phone',ScopeInterface::SCOPE_STORE);

}

public function getEmailUs()
{
    // @see /magento/module-email/etc/config.xml for various emails 
    return $this->scopeConfig->getValue('trans_email/ident_general/email',ScopeInterface::SCOPE_STORE);

}

public function getOpeningHours()
{

    return $this->scopeConfig->getValue('general/store_information/hours',ScopeInterface::SCOPE_STORE);

}

public function getPostcode()
{

    return $this->scopeConfig->getValue('general/store_information/postcode',ScopeInterface::SCOPE_STORE);

}

public function getCity()
{

    return $this->scopeConfig->getValue('general/store_information/city',ScopeInterface::SCOPE_STORE);

}

public function getRegionId()
{

    return $this->scopeConfig->getValue('general/store_information/region_id',ScopeInterface::SCOPE_STORE);

}

public function getStreetLine1()
{
    return $this->scopeConfig->getValue('general/store_information/street_line1',ScopeInterface::SCOPE_STORE);

}

public function getStreetLine2()
{
    return $this->scopeConfig->getValue('general/store_information/street_line2',ScopeInterface::SCOPE_STORE);

}

 }

Include the block in the default.xml by adding the following code

<referenceContainer name="footer">
   <block class="Addpeople\Websettings\Block\Footerlink" name="footerlink" after="footerlogo" template="footerlink.phtml"/>
</referenceContainer>

Please add the following code in footerlink.phtml

<address>
    <span class="displayBlock street-address-1"><?php echo $block->getStreetLine1(); ?></span>
    <span class="displayBlock street-address-2"><?php echo $block->getStreetLine2(); ?></span>
    <span class="displayBlock city"><?php echo $block->getCity();?></span>
    <span class="displayBlock postcode"><?php echo $block->getPostcode();?></span>
    <span class="displayBlock region"><?php echo $block->getRegionId(); ?></span>
</address>