1
namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </type>
</config>

1 Answer 1

2

Make your constructor look like this

public function __construct(Context $context, \Magento\Customer\Model\Session $customerSession)
{
    parent::__construct($context);
    $this->customerSession = $customerSession;
}

and you should not need the markup in di.xml

1
  • 1
    want to understand use of type in di.xml Commented Nov 15, 2021 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.