91

Important reminder: One should never call the Object Manager directly Thus here's how to do it the clean way In any class except templates You first need to inject the following class in your constructor: /Magento/Customer/Model/Session : protected $_session; public function __construct( ... \Magento\Customer\Model\Session $session, ... ) { ...


67

Following code you can check customer login or not anywhere $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('Magento\Customer\Model\Session'); if($customerSession->isLoggedIn()) { // customer login action } From controller $this->_objectManager->get('Magento\Customer\Model\Session'); if($...


33

It is possible via Magento\Framework\App\Http\Context or via Magento\Customer\Model\Session. However, result may be different: HTTP context is initialized earlier than customer session (but it does not matter since both are initialized in action controllers) When PageCache module is on (probably always on production), keep in mind that as soon as layout ...


18

You can Set and get Customer session by using Magento\Customer\Model\Session protected $customerSession; public function __construct( \Magento\Customer\Model\Session $customerSession ){ $this->customerSession = $customerSession; } $this->customerSession->setMyValue('test'); $this->customerSession->getMyValue(); Or by object ...


18

To have access to real data in Magento 2 session you have to apply one of the following Do it on non-cacheable page. Disable full page cache. Set $this->$_isScopePrivate = true for block where you are trying to access session. Why: Magento 2 is rendering public and private content for cacheable pages separately. During cacheable page rendering ...


16

/** @var \Magento\Framework\App\ObjectManager $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\App\Http\Context $context */ $context = $om->get('Magento\Framework\App\Http\Context'); /** @var bool $isLoggedIn */ $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);


11

None of these solutions worked for me. Some pages would appear to be logged in but others would not. It seems this is the bug: https://github.com/magento/magento2/issues/3294 I ended up creating a helper which I could call in my templates: <?php namespace MyVendor\MyModule\Helper; use Magento\Framework\App\Helper\AbstractHelper; /** * Created by ...


9

You need to check customer is loggedin or not. Without customer loggin,you cannot get customer data from session: if (Mage::getSingleton('customer/session')->isLoggedIn()): $customer = Mage::getSingleton('customer/session')->getCustomer(); $customer->getEmail(); $customer->getFirstname(); $customer->getLastname(); $...


9

To get user logged in at template, you can simply call helper in just one single line : <?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()->isLoggedIn(); ?> <?php if( $_loggedin ) : ?> <div><!-- add your code --></div> <?php endif; ?>


8

None of the solutions here worked for me reliably in Magento v2.1 with Full Page Cache and Varnish enabled in Production mode. I finally found a solution that worked 100% of the time with all caching enabled after getting the idea from vendor/magento/module-theme/view/frontend/templates/html/header.phtml. Here is my solution, which shows a "Sign In" link ...


8

I am facing same problem when Cache enable I am not able to get customer session.But I find below solution /** * @var \Magento\Customer\Model\Session */ protected $_customerSession; public function __construct(Template\Context $context, \Magento\Framework\App\Request\Http $request, \Magento\Customer\Api\...


8

Finally got the solution, i am trying my best to explain it. so here's how Magento works !! Magento gets the customer data with passing anything except token value in API call. In between magento calls some API related controllers for the same vendor/magento/module-customer/etc/webapi.xml <route url="/V1/customers/me" method="GET"> <...


7

Try moving your core session initialization above the layout calls: require_once ("/var/www/magento_pack/app/Mage.php"); umask(0); Mage::app("default"); #Mage::setIsDeveloperMode(true); #ini_set('display_errors', 1); umask(0); Mage::app()->loadArea('frontend'); Mage::getSingleton('core/session', array('name' => 'frontend')); $layout = Mage::...


7

If store cache is enable, you can not get customer session. Check below code for get customer session with enable cache. /** * @var \Magento\Customer\Model\Session */ protected $_customerSession; public function __construct(Template\Context $context, \Magento\Framework\App\Request\Http $request, \Magento\Customer\Api\...


7

I have updated my response. In my previous answer, I had a block cacheable=false. This is incorrect because it do the page is not cacheable. For knowing if the customer is logged or not, you must do the next: Namespace/Modulename/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...


7

The best practice is to use Magento\Framework\App\Http\Context to check the customer logged in or not. And, avoiding using Object Manager directly. For example: <?php namespace Vendor\Customer\Block\Account; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Framework\App\Http\Context as ...


6

The similar question was asked many times before and it seems that it stil haven't complete solution. My answer will be based on already solved solutions published here: https://stackoverflow.com/questions/12374289/magento-multiple-websites-share-shopping-cart http://turnkeye.com/blog/share-magento-cart-between-multistores/ If you want to have single ...


6

There are a lot of answers out there that go something like this... GET OBJECT MANAGER LOAD UP CLASS MODEL DO STUFF This is the WRONG methodology to use in Magento2.0. In 2.0, the auto generated object factories are the way to go. You can inject them into your constructor in almost any class and use them. Example: public function __construct( ...


6

Well, you can use either of them. Basically \Magento\Customer\Helper\Session\CurrentCustomer uses \Magento\Customer\Model\Session. The benefit of using the helper class is that it provides extra helper methods to get the current customer data such as: getCustomer : to load the current customer getCustomerId : to get the current customer id The benefit of ...


6

You need to create customer_login event observer for set value in customer session after login 1) Create events.xml app/code/Vendor/Module/etc/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="...


6

You need to use factory method when FPC is enabled. By Factory Method : /** * @var \Magento\Customer\Model\Session */ protected $_customerSessionFactory; public function __construct( .... \Magento\Customer\Model\SessionFactory $customerSessionFactory .... ) { .... $this->_customerSessionFactory = $customerSessionFactory; .... } ...


5

$session->getBeforeAuthUrl(true) gives you the value for before_auth_url and deletes it from the session right after that. For reference see Mage_Core_Model_Session_Abstract_Varien::getData(). If you call the same method without true, you simply get the data value, but this will stay in the session for further use.


5

There are two way Using object manager into your phtml file: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create('Magento\Customer\Model\Session'); if ($customerSession->isLoggedIn()) { $customerSession->getCustomerId(); // get Customer Id $customerSession->...


5

Use below code: $ObjectManager= \Magento\Framework\App\ObjectManager::getInstance(); $context = $ObjectManager->get('Magento\Framework\App\Http\Context'); $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); return $isLoggedIn;


5

Please put below code of paramOverrider in your webapi definition where you need customer Id. <data> <parameter name="customer.id" force="true">%customer_id%</parameter> </data>


4

IIRC this is controllable at the WAF or by enabling some of the options below in System > Configuration > Web > Session Validation


4

There are no exit which function which will do it. Need to add an customer attribute to Customer entity type which will save the telephone to customer table. Which will show on customer registration,edit,checkout form. See how to install an Customer attribute in magento Adding custom attribute to Customer There it should no relation between billing/...


4

Suppose you are deleting session folder, then all login customers & admin users are logout and also cart items are deleted for not login customers (guest) but not effect cart items on login customers because login customers cart items are saved on the database table. (2 & 3) Each file is one person's session and should last no longer than session....


4

Use the below function in your helper file with Magento\Customer\Model\Session to get session object in $customerSession public function getLoggedinCustomerName(){ if($customerSession->getData('customer_id')) { $customer = $this->_customerRepositoryInterface->getById($customerSession->getData('customer_id')); ...


4

Use factory pattern that is bridge to connect incomplete object in magento 2. use below code. I have changed customer model to factory pattern class. That is auto generated code concept of magento 2 you do not need to write any factory class. Magento managed by itself. <?php use Magento\Customer\Model\ResourceModel\CustomerFactory as ...


Only top voted, non community-wiki answers of a minimum length are eligible