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
Asked
Active
Viewed 1,124 times
3 Answers
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.
Sameer Bhayani
- 1,874
- 19
- 34
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
Vishal Baraiya
- 2,816
- 1
- 11
- 22
-
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 05:34
-
ok, check again the link I have to update. – Vishal Baraiya Dec 30 '19 at 05:35
-
Ok thanx, can you also help me to add social icons on header? – Nafsss Dec 30 '19 at 05:37
-
I am happy to working code so accept an answer more discus open tag. – Vishal Baraiya Dec 30 '19 at 05:39
-
Done. Please help me in adding social icons in header – Nafsss Dec 30 '19 at 05:43
-
1next time please ask a new question because that is a good way. so add icon check this link to helpful https://support.weltpixel.com/hc/en-us/articles/222778887-Add-social-links-to-a-static-block-or-page- – Vishal Baraiya Dec 30 '19 at 05:50
-
Let us continue this discussion in chat. – Nafsss Dec 30 '19 at 06:21
-
I think objectmanager should always be discouraged. This is NOT the best way to do this. – Riccardo Dec 30 '19 at 10:45
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);
}
}
Rakesh Donga
- 5,344
- 2
- 24
- 57
Manish Chaubey
- 513
- 2
- 8