0

I want to display email address stored in backend into my header part. How can i get email address into my header.phtml file in magento2

1

objectManager is not recommended so there a better way of implementing it.

Add Below code in phtml file:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('trans_email/ident_general/email');

echo $conf //print email address

More reference check link

| improve this answer | |
  • i dont want to show customer's email address. I want to show stores email address which we store in stores information in admin panel – Nafsss Dec 30 '19 at 5:34
  • ok, check again the link I have to update. – Vishal Baraiya Dec 30 '19 at 5:35
  • Ok thanx, can you also help me to add social icons on header? – Nafsss Dec 30 '19 at 5:37
  • I am happy to working code so accept an answer more discus open tag. – Vishal Baraiya Dec 30 '19 at 5:39
  • 1
    next time please ask a new question because that is a good way. so add icon check this link to helpful support.weltpixel.com/hc/en-us/articles/… – Vishal Baraiya Dec 30 '19 at 5:50
6

Use helper to get email address in phtml. You can do it by following way.

In phtml:

$helper = $this->helper('Vendorname\Modulename\Helper\Data');
$email = $helper->getEmails();
//pass storeId  in getEmails if you want to get email for specific store

In helper define any function, For example getEmails().

<?php
namespace Vendorname\Modulename\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
   public function getEmails($storeId = null) {
      return $this->scopeConfig->getValue('trans_email/ident_general/email', ScopeInterface::SCOPE_STORE, $storeId);
   }
}

I hope this will help you.

| improve this answer | |
1

As per magento2 object manager is bad practice, use ScopeConfigInterface to get backend stored email address. Pass section_id/group_id/field_id from system.xml to get config value. Try below code and please let me know if got any issue.

<?php
namespace Vendorname\Modulename\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Email extends Template
{
protected $scopeConfig;
public function __construct(Template\Context $context, ScopeConfigInterface $scopeConfig)
{
  $this->scopeConfig = $scopeConfig;
  return parent::__construct($context);
}
   public function getEmails() {

   return $this->scopeConfig->getValue('path/of/config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

 }
}
| improve this answer | |

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.