Magento – How to get store admin email address and name in Magento 2 in a Block

blocksemailmagento2

I would like to know the equivalent Magento 2 code to give me access to display these values on the frontend.

/* Name */
Mage::getStoreConfig('trans_email/ident_sales/name'); 

/* Email */
Mage::getStoreConfig('trans_email/ident_sales/email');

How to get store admin email address and name in Magento 2?

I have seen this solution but I do not understand the need for a Controller/Index in order to display values such as Store Email Address.

Best Answer

You have to create first block and defined function inside block for getting store name and store email, inside construct method pass ScopeConfigInterface object

Create the method getStoreName(), getStoreEmail()

    protected $scopeConfig;

    public function __construct(
        .......
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig        
    ) {
        .....
        $this->scopeConfig = $scopeConfig;
        .....
    }
    
    public function getStorename()
    {
        return $this->_scopeConfig->getValue(
            'trans_email/ident_sales/name',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

 public function getStoreEmail()
    {
        return $this->_scopeConfig->getValue(
            'trans_email/ident_sales/email',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

Call function inside your template file, like echo $block->getStoreEmail() and echo $block->getStoreName();

Related Topic