How to get store admin email address and name in Magento 2?
-
14-04-2021 - |
Question
I want replacement of the following in magento 2
/* Name */
Mage::getStoreConfig('trans_email/ident_sales/name');
/* Email */
Mage::getStoreConfig('trans_email/ident_sales/email');
or similar to getting current store admin name and email.
Thanks.
Solution
Try the below code -
<?php
namespace Custom\Module\Controller\Index;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
class Index extends \Magento\Framework\App\Action\Action
{
protected $request;
protected $scopeConfig;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Request\Http $request,
ScopeConfigInterface $scopeConfig
){
parent::__construct($context);
$this->scopeConfig = $scopeConfig;
$this->request = $request;
}
public function execute()
{
$email = $this->scopeConfig->getValue('trans_email/ident_support/email',ScopeInterface::SCOPE_STORE);
$name = $this->scopeConfig->getValue('trans_email/ident_support/name',ScopeInterface::SCOPE_STORE);
echo $email;echo "<br/>";
echo $name;echo "<br/>";
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
OTHER TIPS
Use DI instead of direct Object manager use
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface');
$email = $scopeConfig->getValue('trans_email/ident_support/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange