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 ...
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\...
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, ...
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!...
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 ...
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 $...
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 ...
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 ...
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/...
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
...
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 ...
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);
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
...
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
$...
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, ...
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/...
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 ...
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 ...
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,
...
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\...
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="...
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();
}
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\...
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->...
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');
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/...
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 ...
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->...
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\...
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 (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
customer × 2108magento2 × 880
magento-1.9 × 353
customer-account × 187
login × 134
orders × 113
attributes × 101
customer-attribute × 94
magento-2.1 × 91
event-observer × 86
checkout × 84
magento-1.8 × 77
admin × 77
customer-address × 67
email × 66
magento2.3 × 62
magento-1.7 × 62
database × 60
rest-api × 60
api × 58
php × 55
session × 54
product × 53
register × 52
module × 48