Magento 2 Get customer ID from session in a block classGet customer id from session nullMagento 2 - get user email IDGet Data From SessionUpdate Const template at Magento 2 Classcustomer session does not work except customer page - Magento 2Magento 2: How to override newsletter Subscriber modelError with Dependency Injection in widgetAlthough I use the correct namespace a wrong instance is passed to the constructorMagento get Customer Data from session idMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2 Loading customer through block errorCannot export list of customer from the grid magento 2Magento 2.3 Can't view module's front end page output?
How to manually rewind film?
How can I end combat quickly when the outcome is inevitable?
Geopandas and QGIS Calulating Different Polygon Area Values?
Is a lack of character descriptions a problem?
Generate basis elements of the Steenrod algebra
You have (3^2 + 2^3 + 2^2) Guesses Left. Figure out the Last one
Has there been a multiethnic Star Trek character?
Certain search in list
Why doesn't Adrian Toomes give up Spider-Man's identity?
Which languages would be most useful in Europe at the end of the 19th century?
Is an entry level DSLR going to shoot nice portrait pictures?
Why can my keyboard only digest 6 keypresses at a time?
How can I get an unreasonable manager to approve time off?
What makes Ada the language of choice for the ISS's safety-critical systems?
How is water heavier than petrol, even though its molecular weight is less than petrol?
Winning Strategy for the Magician and his Apprentice
Meaning of 'lose their grip on the groins of their followers'
1980s live-action movie where individually-coloured nations on clouds fight
How did old MS-DOS games utilize various graphic cards?
SQL counting distinct over partition
Did Milano or Benatar approve or comment on their namesake MCU ships?
Why didn't Voldemort recognize that Dumbledore was affected by his curse?
Implement Own Vector Class in C++
Why can't I use =default for default ctors with a member initializer list
Magento 2 Get customer ID from session in a block class
Get customer id from session nullMagento 2 - get user email IDGet Data From SessionUpdate Const template at Magento 2 Classcustomer session does not work except customer page - Magento 2Magento 2: How to override newsletter Subscriber modelError with Dependency Injection in widgetAlthough I use the correct namespace a wrong instance is passed to the constructorMagento get Customer Data from session idMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2 Loading customer through block errorCannot export list of customer from the grid magento 2Magento 2.3 Can't view module's front end page output?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
How to get customer ID from session? I tried this but not work.
protected $_customerBonusPointFactory;
protected $_customerSession;
public function __construct(Session $customerSession, MagentoFrameworkViewElementTemplateContext $context)
$this->_customerSession = $customerSession;
parent::__construct($context);
public function _prepareLayout()
var_dump($this->_customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
magento2 customer
|
show 2 more comments
How to get customer ID from session? I tried this but not work.
protected $_customerBonusPointFactory;
protected $_customerSession;
public function __construct(Session $customerSession, MagentoFrameworkViewElementTemplateContext $context)
$this->_customerSession = $customerSession;
parent::__construct($context);
public function _prepareLayout()
var_dump($this->_customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
magento2 customer
2
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
I just found that$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?
– Paul
Aug 3 '16 at 17:53
3
Block has to be setcacheable=false
see Magento 2 - Get customer ID from session in a block class
– Lukas Komarek
Oct 18 '17 at 11:58
|
show 2 more comments
How to get customer ID from session? I tried this but not work.
protected $_customerBonusPointFactory;
protected $_customerSession;
public function __construct(Session $customerSession, MagentoFrameworkViewElementTemplateContext $context)
$this->_customerSession = $customerSession;
parent::__construct($context);
public function _prepareLayout()
var_dump($this->_customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
magento2 customer
How to get customer ID from session? I tried this but not work.
protected $_customerBonusPointFactory;
protected $_customerSession;
public function __construct(Session $customerSession, MagentoFrameworkViewElementTemplateContext $context)
$this->_customerSession = $customerSession;
parent::__construct($context);
public function _prepareLayout()
var_dump($this->_customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
magento2 customer
magento2 customer
edited Dec 12 '18 at 6:25
Himanshu
1,194723
1,194723
asked Aug 3 '16 at 17:38
PaulPaul
1,87672964
1,87672964
2
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
I just found that$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?
– Paul
Aug 3 '16 at 17:53
3
Block has to be setcacheable=false
see Magento 2 - Get customer ID from session in a block class
– Lukas Komarek
Oct 18 '17 at 11:58
|
show 2 more comments
2
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
I just found that$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?
– Paul
Aug 3 '16 at 17:53
3
Block has to be setcacheable=false
see Magento 2 - Get customer ID from session in a block class
– Lukas Komarek
Oct 18 '17 at 11:58
2
2
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
I just found that
$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?– Paul
Aug 3 '16 at 17:53
I just found that
$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?– Paul
Aug 3 '16 at 17:53
3
3
Block has to be set
cacheable=false
see Magento 2 - Get customer ID from session in a block class– Lukas Komarek
Oct 18 '17 at 11:58
Block has to be set
cacheable=false
see Magento 2 - Get customer ID from session in a block class– Lukas Komarek
Oct 18 '17 at 11:58
|
show 2 more comments
7 Answers
7
active
oldest
votes
It's working copy. You can compare with your block class. Here I use Form as block class
namespace VendorModuleBlock;
class Form extends MagentoFrameworkViewElementTemplate
protected $customerSession;
/**
* Construct
*
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoCustomerModelSession $customerSession
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerModelSession $customerSession,
array $data = []
)
parent::__construct($context, $data);
$this->customerSession = $customerSession;
public function _prepareLayout()
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
1
I did exactly the same but it still return null. And$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.
– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
add a comment |
When You define block which use session You have to disable cache for it.
<block class="VendorModuleBlockIndex" name="Name"
template="Vendor_Module::template/path.phtml" cacheable="false">
</block>
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
add a comment |
You need to inject MagentoCustomerModelSession $customerSession,
class to get customer ID from customer session.
protected $_customerSession;
public function __construct(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->_customerSession = $customerSession;
...
public function getCustomer()
echo $this->_customerSession->getCustomer()->getId(); //Print current customer ID
$customerData = $this->_customerSession->getCustomer();
print_r($customerData->getData()); //Print current Customer Data
NOTE: You only get customer id if customer logged in and customer session initialized
add a comment |
It seems to work when you pass the Context object to the parent class before instantiating the customer session :
class History extends MagentoFrameworkViewElementTemplate
/**
* @var Session
*/
protected $_session;
public function __construct(
TemplateContext $context,
MagentoCustomerModelSession $session,
array $data
)
parent::__construct($context, $data);
$this->_session = $session;
public function _prepareLayout()
var_dump($this->_session->getCustomerId());
exit();
return parent::_prepareLayout();
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
add a comment |
While we are injecting customer session in block to retrive logged in customer data and we are not getting customer data from block because Magento 2 reset all the customer sessions when FPC is enabled.
Please use cacheable="false" for bloick in your layout :
<referenceContainer name="content">
<block class="ArmanTestBlockList" name="list" template="Arman_Test::list.phtml" cacheable="false">
</block>
</referenceContainer>
In this case, Magento 2 ignore this page from caching.
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
add a comment |
If you need only the customer_id
then without loading whole object (see method getCustomer
method) you can get it by simply using getCustomerId
method.
As getId
method also calls getCustomerId
method.
file : vendor/magento/module-customer/Model/Session.php
/**
* Retrieve customer model object
*
* @return Customer
* use getCustomerId() instead
*/
public function getCustomer()
if ($this->_customerModel === null)
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
return $this->_customerModel;
/**
* Retrieve customer id from current session
*
* @api
* @return int|null
*/
public function getCustomerId()
if ($this->storage->getData('customer_id'))
return $this->storage->getData('customer_id');
return null;
/**
* Retrieve customer id from current session
*
* @return int|null
*/
public function getId()
return $this->getCustomerId();
add a comment |
First, create an instance in header.phtml file as below and also if more than one store is available and one wants to get mail in only one of the stores.
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
?>
<?php
$customerSession = $om->get('MagentoCustomerModelSession');
if($customerSession->isLoggedIn())
echo $customerSession->getCustomer()->getId(); // get ID
?>
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f128830%2fmagento-2-get-customer-id-from-session-in-a-block-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's working copy. You can compare with your block class. Here I use Form as block class
namespace VendorModuleBlock;
class Form extends MagentoFrameworkViewElementTemplate
protected $customerSession;
/**
* Construct
*
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoCustomerModelSession $customerSession
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerModelSession $customerSession,
array $data = []
)
parent::__construct($context, $data);
$this->customerSession = $customerSession;
public function _prepareLayout()
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
1
I did exactly the same but it still return null. And$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.
– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
add a comment |
It's working copy. You can compare with your block class. Here I use Form as block class
namespace VendorModuleBlock;
class Form extends MagentoFrameworkViewElementTemplate
protected $customerSession;
/**
* Construct
*
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoCustomerModelSession $customerSession
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerModelSession $customerSession,
array $data = []
)
parent::__construct($context, $data);
$this->customerSession = $customerSession;
public function _prepareLayout()
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
1
I did exactly the same but it still return null. And$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.
– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
add a comment |
It's working copy. You can compare with your block class. Here I use Form as block class
namespace VendorModuleBlock;
class Form extends MagentoFrameworkViewElementTemplate
protected $customerSession;
/**
* Construct
*
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoCustomerModelSession $customerSession
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerModelSession $customerSession,
array $data = []
)
parent::__construct($context, $data);
$this->customerSession = $customerSession;
public function _prepareLayout()
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
It's working copy. You can compare with your block class. Here I use Form as block class
namespace VendorModuleBlock;
class Form extends MagentoFrameworkViewElementTemplate
protected $customerSession;
/**
* Construct
*
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoCustomerModelSession $customerSession
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerModelSession $customerSession,
array $data = []
)
parent::__construct($context, $data);
$this->customerSession = $customerSession;
public function _prepareLayout()
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
edited Jan 16 '17 at 20:48
P0lT10n
1054
1054
answered Aug 3 '16 at 18:10
Sohel RanaSohel Rana
24.3k34664
24.3k34664
1
I did exactly the same but it still return null. And$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.
– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
add a comment |
1
I did exactly the same but it still return null. And$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.
– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
1
1
I did exactly the same but it still return null. And
$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.– Paul
Aug 3 '16 at 18:21
I did exactly the same but it still return null. And
$this->customerSession->isLoggedIn()
is false always. I do the same in a controller class and it works fine.– Paul
Aug 3 '16 at 18:21
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
Finally, it works. I'm not sure what I have changed.
– Paul
Aug 5 '16 at 9:49
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
have you disabled full page cache maybe?
– davideghz
Mar 22 '17 at 8:32
Yes it was cache I've had same issue
<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
Yes it was cache I've had same issue
<block class="VendorBlockBlaBla" name="block.name" template="Wed2b_Suppliers::template/template.phtml" cacheable="false"/>
– Juliano Vargas
Aug 9 '18 at 14:27
add a comment |
When You define block which use session You have to disable cache for it.
<block class="VendorModuleBlockIndex" name="Name"
template="Vendor_Module::template/path.phtml" cacheable="false">
</block>
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
add a comment |
When You define block which use session You have to disable cache for it.
<block class="VendorModuleBlockIndex" name="Name"
template="Vendor_Module::template/path.phtml" cacheable="false">
</block>
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
add a comment |
When You define block which use session You have to disable cache for it.
<block class="VendorModuleBlockIndex" name="Name"
template="Vendor_Module::template/path.phtml" cacheable="false">
</block>
When You define block which use session You have to disable cache for it.
<block class="VendorModuleBlockIndex" name="Name"
template="Vendor_Module::template/path.phtml" cacheable="false">
</block>
answered Dec 5 '17 at 12:15
Marcin ŻurekMarcin Żurek
311
311
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
add a comment |
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
1
1
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
this will cause the whole page and every page that use this block will be MISSed by the FPC
– Doni Wibowo
May 7 '18 at 9:47
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
@DoniWibowo that is true, but you need to be carefull when caching pages with dynamic data in the first place. You don't want to display the same name for all customers for example.
– Radu
Oct 3 '18 at 15:03
add a comment |
You need to inject MagentoCustomerModelSession $customerSession,
class to get customer ID from customer session.
protected $_customerSession;
public function __construct(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->_customerSession = $customerSession;
...
public function getCustomer()
echo $this->_customerSession->getCustomer()->getId(); //Print current customer ID
$customerData = $this->_customerSession->getCustomer();
print_r($customerData->getData()); //Print current Customer Data
NOTE: You only get customer id if customer logged in and customer session initialized
add a comment |
You need to inject MagentoCustomerModelSession $customerSession,
class to get customer ID from customer session.
protected $_customerSession;
public function __construct(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->_customerSession = $customerSession;
...
public function getCustomer()
echo $this->_customerSession->getCustomer()->getId(); //Print current customer ID
$customerData = $this->_customerSession->getCustomer();
print_r($customerData->getData()); //Print current Customer Data
NOTE: You only get customer id if customer logged in and customer session initialized
add a comment |
You need to inject MagentoCustomerModelSession $customerSession,
class to get customer ID from customer session.
protected $_customerSession;
public function __construct(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->_customerSession = $customerSession;
...
public function getCustomer()
echo $this->_customerSession->getCustomer()->getId(); //Print current customer ID
$customerData = $this->_customerSession->getCustomer();
print_r($customerData->getData()); //Print current Customer Data
NOTE: You only get customer id if customer logged in and customer session initialized
You need to inject MagentoCustomerModelSession $customerSession,
class to get customer ID from customer session.
protected $_customerSession;
public function __construct(
...
MagentoCustomerModelSession $customerSession,
...
)
...
$this->_customerSession = $customerSession;
...
public function getCustomer()
echo $this->_customerSession->getCustomer()->getId(); //Print current customer ID
$customerData = $this->_customerSession->getCustomer();
print_r($customerData->getData()); //Print current Customer Data
NOTE: You only get customer id if customer logged in and customer session initialized
edited Jul 22 '17 at 5:18
answered Apr 26 '17 at 9:11
Prince PatelPrince Patel
14.1k65884
14.1k65884
add a comment |
add a comment |
It seems to work when you pass the Context object to the parent class before instantiating the customer session :
class History extends MagentoFrameworkViewElementTemplate
/**
* @var Session
*/
protected $_session;
public function __construct(
TemplateContext $context,
MagentoCustomerModelSession $session,
array $data
)
parent::__construct($context, $data);
$this->_session = $session;
public function _prepareLayout()
var_dump($this->_session->getCustomerId());
exit();
return parent::_prepareLayout();
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
add a comment |
It seems to work when you pass the Context object to the parent class before instantiating the customer session :
class History extends MagentoFrameworkViewElementTemplate
/**
* @var Session
*/
protected $_session;
public function __construct(
TemplateContext $context,
MagentoCustomerModelSession $session,
array $data
)
parent::__construct($context, $data);
$this->_session = $session;
public function _prepareLayout()
var_dump($this->_session->getCustomerId());
exit();
return parent::_prepareLayout();
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
add a comment |
It seems to work when you pass the Context object to the parent class before instantiating the customer session :
class History extends MagentoFrameworkViewElementTemplate
/**
* @var Session
*/
protected $_session;
public function __construct(
TemplateContext $context,
MagentoCustomerModelSession $session,
array $data
)
parent::__construct($context, $data);
$this->_session = $session;
public function _prepareLayout()
var_dump($this->_session->getCustomerId());
exit();
return parent::_prepareLayout();
It seems to work when you pass the Context object to the parent class before instantiating the customer session :
class History extends MagentoFrameworkViewElementTemplate
/**
* @var Session
*/
protected $_session;
public function __construct(
TemplateContext $context,
MagentoCustomerModelSession $session,
array $data
)
parent::__construct($context, $data);
$this->_session = $session;
public function _prepareLayout()
var_dump($this->_session->getCustomerId());
exit();
return parent::_prepareLayout();
answered Oct 7 '16 at 15:19
ptidusptidus
367
367
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
add a comment |
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
2
2
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
Odd. I observe the same thing. Thank you for the help. I wonder why this makes a difference.
– nshiff
Apr 4 '17 at 15:22
add a comment |
While we are injecting customer session in block to retrive logged in customer data and we are not getting customer data from block because Magento 2 reset all the customer sessions when FPC is enabled.
Please use cacheable="false" for bloick in your layout :
<referenceContainer name="content">
<block class="ArmanTestBlockList" name="list" template="Arman_Test::list.phtml" cacheable="false">
</block>
</referenceContainer>
In this case, Magento 2 ignore this page from caching.
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
add a comment |
While we are injecting customer session in block to retrive logged in customer data and we are not getting customer data from block because Magento 2 reset all the customer sessions when FPC is enabled.
Please use cacheable="false" for bloick in your layout :
<referenceContainer name="content">
<block class="ArmanTestBlockList" name="list" template="Arman_Test::list.phtml" cacheable="false">
</block>
</referenceContainer>
In this case, Magento 2 ignore this page from caching.
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
add a comment |
While we are injecting customer session in block to retrive logged in customer data and we are not getting customer data from block because Magento 2 reset all the customer sessions when FPC is enabled.
Please use cacheable="false" for bloick in your layout :
<referenceContainer name="content">
<block class="ArmanTestBlockList" name="list" template="Arman_Test::list.phtml" cacheable="false">
</block>
</referenceContainer>
In this case, Magento 2 ignore this page from caching.
While we are injecting customer session in block to retrive logged in customer data and we are not getting customer data from block because Magento 2 reset all the customer sessions when FPC is enabled.
Please use cacheable="false" for bloick in your layout :
<referenceContainer name="content">
<block class="ArmanTestBlockList" name="list" template="Arman_Test::list.phtml" cacheable="false">
</block>
</referenceContainer>
In this case, Magento 2 ignore this page from caching.
answered May 16 '18 at 5:37
Khan armanKhan arman
1014
1014
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
add a comment |
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
how to use cacheable="false" in cms pages?
– jafar pinjar
Mar 22 at 7:20
add a comment |
If you need only the customer_id
then without loading whole object (see method getCustomer
method) you can get it by simply using getCustomerId
method.
As getId
method also calls getCustomerId
method.
file : vendor/magento/module-customer/Model/Session.php
/**
* Retrieve customer model object
*
* @return Customer
* use getCustomerId() instead
*/
public function getCustomer()
if ($this->_customerModel === null)
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
return $this->_customerModel;
/**
* Retrieve customer id from current session
*
* @api
* @return int|null
*/
public function getCustomerId()
if ($this->storage->getData('customer_id'))
return $this->storage->getData('customer_id');
return null;
/**
* Retrieve customer id from current session
*
* @return int|null
*/
public function getId()
return $this->getCustomerId();
add a comment |
If you need only the customer_id
then without loading whole object (see method getCustomer
method) you can get it by simply using getCustomerId
method.
As getId
method also calls getCustomerId
method.
file : vendor/magento/module-customer/Model/Session.php
/**
* Retrieve customer model object
*
* @return Customer
* use getCustomerId() instead
*/
public function getCustomer()
if ($this->_customerModel === null)
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
return $this->_customerModel;
/**
* Retrieve customer id from current session
*
* @api
* @return int|null
*/
public function getCustomerId()
if ($this->storage->getData('customer_id'))
return $this->storage->getData('customer_id');
return null;
/**
* Retrieve customer id from current session
*
* @return int|null
*/
public function getId()
return $this->getCustomerId();
add a comment |
If you need only the customer_id
then without loading whole object (see method getCustomer
method) you can get it by simply using getCustomerId
method.
As getId
method also calls getCustomerId
method.
file : vendor/magento/module-customer/Model/Session.php
/**
* Retrieve customer model object
*
* @return Customer
* use getCustomerId() instead
*/
public function getCustomer()
if ($this->_customerModel === null)
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
return $this->_customerModel;
/**
* Retrieve customer id from current session
*
* @api
* @return int|null
*/
public function getCustomerId()
if ($this->storage->getData('customer_id'))
return $this->storage->getData('customer_id');
return null;
/**
* Retrieve customer id from current session
*
* @return int|null
*/
public function getId()
return $this->getCustomerId();
If you need only the customer_id
then without loading whole object (see method getCustomer
method) you can get it by simply using getCustomerId
method.
As getId
method also calls getCustomerId
method.
file : vendor/magento/module-customer/Model/Session.php
/**
* Retrieve customer model object
*
* @return Customer
* use getCustomerId() instead
*/
public function getCustomer()
if ($this->_customerModel === null)
$this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
return $this->_customerModel;
/**
* Retrieve customer id from current session
*
* @api
* @return int|null
*/
public function getCustomerId()
if ($this->storage->getData('customer_id'))
return $this->storage->getData('customer_id');
return null;
/**
* Retrieve customer id from current session
*
* @return int|null
*/
public function getId()
return $this->getCustomerId();
answered Jan 16 at 10:48
Knight017Knight017
387212
387212
add a comment |
add a comment |
First, create an instance in header.phtml file as below and also if more than one store is available and one wants to get mail in only one of the stores.
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
?>
<?php
$customerSession = $om->get('MagentoCustomerModelSession');
if($customerSession->isLoggedIn())
echo $customerSession->getCustomer()->getId(); // get ID
?>
add a comment |
First, create an instance in header.phtml file as below and also if more than one store is available and one wants to get mail in only one of the stores.
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
?>
<?php
$customerSession = $om->get('MagentoCustomerModelSession');
if($customerSession->isLoggedIn())
echo $customerSession->getCustomer()->getId(); // get ID
?>
add a comment |
First, create an instance in header.phtml file as below and also if more than one store is available and one wants to get mail in only one of the stores.
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
?>
<?php
$customerSession = $om->get('MagentoCustomerModelSession');
if($customerSession->isLoggedIn())
echo $customerSession->getCustomer()->getId(); // get ID
?>
First, create an instance in header.phtml file as below and also if more than one store is available and one wants to get mail in only one of the stores.
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
?>
<?php
$customerSession = $om->get('MagentoCustomerModelSession');
if($customerSession->isLoggedIn())
echo $customerSession->getCustomer()->getId(); // get ID
?>
answered May 31 at 8:17
SHEESHRAMSHEESHRAM
586
586
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f128830%2fmagento-2-get-customer-id-from-session-in-a-block-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
If customer logged in then you can get customer id otherwise it's return null using '$this->_customerSession->getCustomer()->getId()'
– Sohel Rana
Aug 3 '16 at 17:48
I have logged in but it returns null. And I'm doing it in the block class.
– Paul
Aug 3 '16 at 17:50
Which session class you use?
– Sohel Rana
Aug 3 '16 at 17:51
I just found that
$this->session->isLoggedIn()
return true in my controller class but return false in my block class. Why?– Paul
Aug 3 '16 at 17:53
3
Block has to be set
cacheable=false
see Magento 2 - Get customer ID from session in a block class– Lukas Komarek
Oct 18 '17 at 11:58