Skip to main content
113 votes

How to check if customer is logged in or not?

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 ...
Raphael at Digital Pianism's user avatar
48 votes
Accepted

Magento 2 customer-data.js:90:23 [object Object]

I have literally wasted a few days trying to debug this issue and finally got a definite solution or work around I would call it. Hopefully it can save a few other people the headache I currently have!...
RLTcode's user avatar
  • 863
41 votes
Accepted

Magento 2 Get customer ID from session in a block class

It's working copy. You can compare with your block class. Here I use Form as block class namespace Vendor\Module\Block; class Form extends \Magento\Framework\View\Element\Template { protected $...
Sohel Rana's user avatar
  • 35.9k
41 votes

Magento 2 - Redirect customer to custom page after login

A plugin is a better solution in this case because your extended class might need to be updated when Magento 2 updates. Here is a solution using a after-plugin on LoginPost->execute() as suggested by ...
cjohansson's user avatar
38 votes
Accepted

Magento2 : Best way to load Customer by customer Id

It's always a better practice to use service contracts. In your case I would use \Magento\Customer\Api\CustomerRepositoryInterface : protected $_customerRepositoryInterface; public function ...
Raphael at Digital Pianism's user avatar
31 votes

Magento 2: How to show address field in registration form?

Example display address fields using customer_account_create.xml: <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/...
wit's user avatar
  • 311
25 votes
Accepted

Magento 2 Check If Customer is Subscribed to Newsletter

First inject the \Magento\Newsletter\Model\Subscriber class in your constructor: protected $_subscriber; public function __construct( ... \Magento\Newsletter\Model\Subscriber $subscriber ...
Raphael at Digital Pianism's user avatar
22 votes
Accepted

customer session does not work except customer page - Magento 2

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 ...
Sergii Ivashchenko's user avatar
22 votes
Accepted

how to load customer by email in magento2?

In Magento 2 you need to use the get method from the Magento\Customer\Api\CustomerRepositoryInterface service contract class. public function get($email, $websiteId = null);
Raphael at Digital Pianism's user avatar
21 votes
Accepted

Magento 2: How to get order information using REST API?

For order information: $userData = array("username" => "admin", "password" => "admin123"); $ch = curl_init("http://magento213/index.php/rest/V1/integration/admin/token"); curl_setopt($ch, ...
Sohel Rana's user avatar
  • 35.9k
21 votes
Accepted

Magento 2: Get Customer data after login with observer

Use customer_login event observer for customer login action 1) Create events.xml app/code/Vendor/Module/etc/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/...
Prince Patel's user avatar
  • 22.8k
20 votes

“We can't save the customer” Alert on Magento 2

You most probably have an error somewhere. In order to find out what the problem is you can add this code. $this->messageManager->addException($e, $e->getMessage()); Instead of the line $...
Marius's user avatar
  • 198k
20 votes
Accepted

How to block/remove registration of new customers in Magento 2?

Magento 2 has a system in place to facilitate disabling customer registration. The customer module includes a model of which it's the sole purpose to return a boolean true or false to indicate if ...
7ochem's user avatar
  • 7,562
19 votes

How to check if customer is logged in or not?

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 ...
Carl Owens's user avatar
18 votes
Accepted

Magento2 How to get customer group name by using group id?

If you have group id then add below code at your block class protected $groupRepository; public function __construct( ... \Magento\Customer\Api\GroupRepositoryInterface $groupRepository, ...
Amit Bera's user avatar
  • 77.6k
18 votes

Disable customer attribute in magento admin form

You need to create Namespace/Module/view/base/ui_component/customer_form.xml and add the following code <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="...
Shankar's user avatar
  • 442
17 votes
Accepted

In Magento 2 - How to remove a column in customer_entity table?

In your setup script, we can use dropColumn: $setup->getConnection()->dropColumn($setup->getTable('your_table'), 'your_column');
Khoa TruongDinh's user avatar
17 votes
Accepted

How to prevent "Call to a member function getValue() on null" when using getCustomAttribute()->getValue()?

Try following way: if($myCustomAttribute = $customer->getCustomAttribute('my_custom_attribute')) { echo $myCustomAttribute->getValue(); }
Sohel Rana's user avatar
  • 35.9k
16 votes
Accepted

How to get all list of customer group?

I was able to get solution using the below code: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $groupOptions = $objectManager->get('\Magento\Customer\Model\ResourceModel\...
Prashant Valanda's user avatar
16 votes
Accepted

Magento 2 How to select fields in query

Try following way: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->...
Sohel Rana's user avatar
  • 35.9k
16 votes
Accepted

Magento 2: Save custom customer attribute value programmatically

I have got the solution protected $customer; protected $customerFactory; public function __construct( \Magento\Customer\Model\Customer $customer, \Magento\Customer\Model\ResourceModel\...
Dinesh Yadav's user avatar
  • 6,465
16 votes
Accepted

Customer custom attribute not showing in admin form

In contrast to Magento 1, in Magento 2 you now need to add some XML to make custom attributes appear in the edit customer section of the admin. Create YourNamespace/YourModule/view/base/ui_component/...
ProcessEight's user avatar
  • 1,000
15 votes

How do I apply coupon codes for newly registered customers only in Magento 1.9?

I'll create a module which does this in order to give more insight into how it's done. We're going to use setup scripts (for attribute setup), event observers, condition model and a helper class. I'm ...
versedi's user avatar
  • 2,059
15 votes
Accepted

magento2 check if customer is logged in or not in knockout template

You need to pass dependency of customer object, 'Magento_Customer/js/model/customer', define( [ 'ko', 'jquery', 'Magento_Customer/js/model/customer', 'mage/translate' ], function (...
Rakesh Jesadiya's user avatar
14 votes

How to check if customer is logged in or not?

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 ...
thdoan's user avatar
  • 728
14 votes
Accepted

How to add programmatically product to wishlist in Magento 2?

To do so, you will first have to inject the following in your constructor: Magento\Wishlist\Model\WishlistFactory Magento\Catalog\Api\ProductRepositoryInterface So update your class constructor like ...
Raphael at Digital Pianism's user avatar
13 votes

How to check if customer is logged in or not?

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()-&...
Afham's user avatar
  • 366
13 votes
Accepted

Magento 2: Restrict customer registration by specific mail extension

In your case, I think we can try with Plugin. The Magento\Customer\Controller\Account\CreatePost controller will execute the customer registration request. So, we can build the logic with this class. ...
Khoa TruongDinh's user avatar
13 votes

Magento2 : customer-data.js:86 Uncaught Error: [object Object]

Sometimes this is caused by something as disabling a (payment) module the wrong way and RLTcode's answer is very useful. However, in my case the mini cart was not updated when a product was added. No ...
Isolde's user avatar
  • 451
12 votes

How to check if customer is logged in or not?

Another answer: <?php $_loggedin = $block->getLayout()->createBlock('Magento\Customer\Block\Account\AuthorizationLink')->isLoggedIn() ?> <?php if( $_loggedin ) : ?> // your ...
xanka's user avatar
  • 2,114

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