:::: MENU ::::

Get customer session data with cache in block

Recent QuestionsGet customer session data with cache in block
Abdus Salam asked 3 weeks ago

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

1 Answers
Best Answer
qaisarsatti Staff answered 3 weeks 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;
    }

}