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
82 votes
Accepted

How to check if customer is logged in or not?

Following code you can check customer login or not anywhere $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('Magento\Customer\Model\...
Sohel Rana's user avatar
  • 35.6k
49 votes
Accepted

How do I remove customers middle initial/name from the checkout page?

How the Magento 1.9.2.1 update affects Middle Name First, the solution I had the same issue when updating to Magento CE 1.9.2.1 from Magento CE 1.9.2.0. The update caused Middle Name to display, ...
user31617's user avatar
  • 506
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
44 votes

How to check if customer is logged in or not?

It is possible via Magento\Framework\App\Http\Context or via Magento\Customer\Model\Session. However, the result may be different: HTTP context is initialized earlier than the customer session (but ...
Alex Paliarush's user avatar
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.6k
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
36 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

After applying SUPEE-6788 can't create customer

The patch adds a new form key to app/design/frontend/base/default/template/customer/form/register.phtml and to app/design/frontend/base/default/template/persistent/customer/form/register.phtml ...
JamesAllwood's user avatar
  • 1,543
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

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.6k
20 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.6k
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
19 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,502
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.2k
17 votes

How to check if customer is logged in or not?

/** @var \Magento\Framework\App\ObjectManager $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\App\Http\Context $context */ $context = $om->get('Magento\...
Dmitrii Fediuk's user avatar
17 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
  • 432
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.6k
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.6k
16 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
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,069
15 votes

Magento 2 - Change customer password programmatically

The save method of the class Magento\Customer\Model\ResourceModel\CustomerRepository has an extra argument passwordHash Use this argument to set a new password: $customer = $this->...
Vladimir Kerkhoff's user avatar
15 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,397
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

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