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>
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 Nov 15 '21 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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