:::: MENU ::::

Get customer session data with cache in block

Recent QuestionsGet customer session data with cache in block
Abdus Salam asked 11 months ago

how to use customer sessions in block while cache is enabled in magento2

0 Answers
qaisarsatti Staff answered 11 months ago

You can get session data in cache using obejct manager. So using following code you can get customer session data with cache enabled

namespace QaisarSatti\Module\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{

  protected $objectmanager;  


  public function __construct(
     
        \Magento\Framework\ObjectManagerInterface $objectmanager

    ) {


        $this->objectmanager = $objectmanager;
     
    }
    public function getCustomerData()
    {
         $customerSession = $this->objectmanager->create('Magento\Customer\Model\SessionFactory')->create();
         return $customerSession;
    }

}