0

Edit.php (block)

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

    protected $request;

    protected $customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = [],
        \FM\Quick\Model\QuickFactory $modelFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Request\Http $request




    ) {
        parent::__construct($context,$data);
        $this->_modelFactory = $modelFactory;
         $this->request = $request;
        $this->customerSession = $customerSession;

    }




    public function getIddata()
    {
        $this->request->getParams(); 
        return $this->request->getParam('id');
    }


    public function getCollection(){

        $id=$this->getIddata();
        $customer_email=$this->getCustomer();  //here i'm getting null


        $d=$this->_modelFactory->create();
        return $d->getCollection()->addFieldToFilter('quick_id',$id);

    }

    public function getCustomer()
    {
        $customerData=$this->customerSession->getCustomer();

        return $customerData->getEmail();

    }

}

$customer_email=$this->getCustomer(); //here i'm getting null

getting null on this variable

why is not getting customer email in Magento 2?

1

Try this. Ref. https://sohel.dev/2017/05/05/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/

namespace SR\CustomerSession\Model\Customer;

class Context
{
    /**
     * Customer authorization cache context
     */
    const CONTEXT_CUSTOMER_ID = 'logged_in_customer_id';
}

di.xml [SR/CustomerSession/etc/frontend/di.xml]

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Action\AbstractAction">
        <plugin name="sr-customer-app-action-dispatchController-context-plugin" type="SR\CustomerSession\Plugin\App\Action\Context" sortOrder="15"/>
    </type>
</config>

create SR/CustomerSession/Plugin/App/Action/Context.php

namespace SR\CustomerSession\Plugin\App\Action;

use SR\CustomerSession\Model\Customer\Context as CustomerSessionContext;

class Context
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Framework\App\Http\Context $httpContext
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->customerSession = $customerSession;
        $this->httpContext = $httpContext;
    }

    /**
     * @param \Magento\Framework\App\ActionInterface $subject
     * @param callable $proceed
     * @param \Magento\Framework\App\RequestInterface $request
     * @return mixed
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundDispatch(
        \Magento\Framework\App\ActionInterface $subject,
        \Closure $proceed,
        \Magento\Framework\App\RequestInterface $request
    ) {
        $customerId = $this->customerSession->getCustomerId();
        if(!$customerId) {
            $customerId = 0;
        }

        $this->httpContext->setValue(
            CustomerSessionContext::CONTEXT_CUSTOMER_ID,
            $customerId,
            false
        );

        return $proceed($request);
    }
}

Now you can able to get customer id following way even cache enable:

/**
* @var \Magento\Framework\App\Http\Context
*/
$this->httpContext->getValue(SR\CustomerSession\Model\Customer\Context::CONTEXT_CUSTOMER_ID);
1

Replace your block code with the following code:

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

    protected $request;

    protected $customerSession;

    protected $objectManager;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = [],
        \FM\Quick\Model\QuickFactory $modelFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\ObjectManagerInterface $objectManager




    ) {
        parent::__construct($context,$data);
        $this->_modelFactory = $modelFactory;
         $this->request = $request;
        $this->customerSession = $customerSession;
        $this->objectManager = $objectManager;

    }

    public function _prepareLayout()
   { 
        parent::_prepareLayout();
        $this->getCustomerSession();

        return $this;
   }


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

    public function getIddata()
    {
        $this->request->getParams(); 
        return $this->request->getParam('id');
    }


    public function getCollection(){

        $id=$this->getIddata();
        $customer_email=$this->getCustomer();  //here i'm getting null


        $d=$this->_modelFactory->create();
        return $d->getCollection()->addFieldToFilter('quick_id',$id);

    }

    public function getCustomer()
    {
        $customerData=$this->customerSession->getCustomer();

        return $customerData->getEmail();

    }
 }

Also, run compile command and delete the generated folder, hope it works

  • Does it work for you? – ASQ Aug 20 at 6:13
0

Try This :-

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->_customerSession = $customerSession->create();
        parent::__construct($context, $data);
    } 
    public function getCustomerData() {
        if ($this->_customerSession->isLoggedIn()) {
            $customer = $this->_customerSession->getCustomerData();
            $customerEmail = $customerData->getEmail();
        }
    }

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.