Create Helper in magento 2 - Magesan

Create Helper in magento 2

Create Helper in magento 2

Helpers are very useful and needful elements. And beside this you can inject any dependency and call it to anywhere you need.

You can create Helpers like this….

<?php

namespace Magesan\Extension\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
	/** /Magento\Framework\App\Config\ScopeConfigInterface $_scopeConfig */
	protected $_scopeConfig;

	/** /Magento\Store\Model\StoreManagerInterface $_storeManager */
	protected $_storeManager;

    /**
     * __construct function
     *
     * @param ScopeConfigInterface $scopeConfig
     * @param StoreManagerInterface $storeManager
    */
	public function __construct(
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;
    }

    /**
     * getScopeConfig function
     *
     * @return void
     */
   	public function getScopeConfig($path, $storeId = null)
	{
	   $storeId = $this->getStoreId();

	    return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
	}

    /**
     * getStoreId function
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }
}

4 thoughts on “Create Helper in magento 2

  • Hairstyles
    09/10/2021 at 7:32 PM

    Thanks for your article.

  • Freebies
    14/10/2021 at 5:03 AM

    Cool.

  • Hairstyles
    20/10/2021 at 9:43 PM

    I have been examinating out a few of your articles and i must say clever stuff. I will surely bookmark your blog.

  • Hairstyles
    06/12/2021 at 2:29 AM

    Wow! This could be one particular of the most helpful blogs We’ve ever arrive across on this subject. Basically Magnificent. I’m also an expert in this topic so I can understand your hard work.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>