Thursday , May 9 2024
Home > Magento Tips & News > Magento 2 Tutorials > Sales > How to Login as Customer in Magento Open Source

How to Login as Customer in Magento Open Source

Previously in our Magento Sales tutorials, we illustrated how store owners provide customers with remote shopping assistance in Magento Commerce Version. What about Open Source? Is it equipped with this functionality that helps customers get it sorted while shopping? 

Since the release of Magento 2.4, this function has been made available for Open Source by the name of Login as Customer. 

Without further ado, we will guide you through the steps to Log in as a Customer in Magento 2. Let’s start!

Login as Customer in Magento Old Versions 

If you haven’t updated the Magento 2.4.x version yet, some “hacks” still help. Including: 

Use Some Code

Magento newbies may find it hard when it comes to code. If you are among them and you also hate something complicated, skip this section. Otherwise, follow these steps: 

  1. Everything you need is a customer ID 
  2. First, get a Customer object using CustomerRepositoryInterface interface. Use Customer Object to enter your customer session with setCustomerDataAsLoggedIn function.
  3. Apply the following code lines: 
<?php
use Exception;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerLogin {

    public function __construct(
        CustomerRepositoryInterface $customerRepository,
        CustomerSession $customerSession
    ) {
        $this->customerRepository = $customerRepository;
        $this->customerSession = $customerSession;
    }

    /**
     * Login Customer by Customer Id
     *
     * @param int $customerId
     * @return bool
     * @throws Exception
     */
    public function loginCustomerById(int $customerId)
    {
        try {
            // @var CustomerRepositoryInterface $customer
            $customer = $this->customerRepository->getById($customerId);
        } catch (NoSuchEntityException $exception) {
            throw new Exception(__('The wrong customer account is specified.'));
        }

        $this->customerSession->setCustomerDataAsLoggedIn($customer);
        return true;
    }
}

Done! Now, you can log into the customer account without going to the login page. 

Use Login as Customer extensions

It’s not a promotion but we highly recommend you use our Login as Customer extension if you use an old version of Magento. Why? 

It’s Totally Free! And it’s quick. The light code package will not slow down your site. 

Moreover, admins can easily login into clients’ accounts from the backend without asking for their usernames and password. 

Highlight features that help store owners efficiently deal with issues related to accounts and the shopping process: 

  • Log in to customer accounts in one click
  • Track and record all login actions
  • Limit login permission to specific sub-admin users

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to build or optimize your Magento stores for sales-boosting? Then visit the Magezon website and grab the necessary add-ons for yours today!

Magento 2 Log in as Customer – Enable the Feature

Enable Login as Customers 

  1. From the admin interface, navigate to Stores >> Settings >> Configuration
  2. In the left sidebar, expand the Customers section, then select Login as Customer. 
Magento 2 login as customer - enable

Config Explanation

  • Enable Login as Customer: The Yes/No field lets the admin enable/disable the feature. 
  • Disable Page Cache For Admin User: Another Yes/No field. If the value is Yes, page caching is disabled when the Admin user is logged in as a customer.
  • Store View To Login To An optional field with 2 values: Auto-Detection & Manual Selection. The default value is set to Auto-Detection, which automatically picks up the right store view depending on locations. The Manual Selection is often used for global e-stores and the admin can select a store manually as long as the Share Customer Accounts feature is enabled. 
  • Title for Login as Customer opt-in checkbox: This field allows the admin to define the checkbox title displayed in the customer’s account information section. 
Magento 2 login as customer - Enable
  • Login as Customer checkbox tooltip: Enter the checkbox description which is popped out when hovering over the question mark circle. 
Magento 2 login as customer - Tool tip

By default (System Value), the configuration is set like below: 

Magento 2 login as customer - default values

If you wanna change any settings inside, untick the checkbox Use system value

3. After finishing all the configurations, hit the Save Config button and Reload Cache. 

Utilize Login as Customer

The Login as Customer function allows store owners to troubleshoot or take actions on behalf of customers. This may lead to some inconvenience while accessing personal information like bill address, payment method, card info, etc. 

Hence, from Magento 2.4.1 onward, you may need customers’ confirmation before logging in to protect the customers’ security and privacy. If they don’t tick the Allow remote shopping assistance checkbox, they have no right to access their account. Also, this feature requires admin with the ability to approach the path Customers >> Login as Customer. 

magento 2 login as customer: edit account info

If the option is not enabled from the customers’ side, an error message will appear like the one below when you try to enter their account details. 

Magento 2 login as customer - error message

To save time and skip this extra step, store owners can disable the module LoginAsCustomerAssistance via this command line:

php bin/magento module:disable Magento_LoginAsCustomerAssistance

Then refresh the site.

Then, the checkbox Allow remote shopping assistance will disappear from the customer’s account dashboard. 

Login as Customer Appearances

Login as Customer button is displayed on the following pages:

  • Customer Edit Interface

Enter the Customer Edit Interface via this path: Customers >> All Customers >> Pick one customer and Edit his/her profile

Magento 2 login as customer - button on customer pro5
  • Order View Interface

Go to Sales >> Orders from the Admin sidebar. Pick one order that needs to be edited from the grid. Hit View in the Action column. Then, hit the Login as customer button on the top bar. 

Magento 2 login as customer - button 2
  • Invoice View Interface
  • Shipment View Interface
  • Credit Memo View Interface

The previous blog Magento 2 Shopping assistance for Adobe Commerce, illustrates all the common shopping assistance. In Magento Open Source, the configuration steps are the same. 

End Assisting Session

From the storefront, login as a customer connection will be displayed by a message at the page header. To end this session, hit the Close session button and flush related caches. 

Final Words

That’s all about how to log in on behalf of customers in Magento Open Source. This feature really helps when the admin wants to manage customers’ shopping carts or give them some support. Visit our Magento tutorial series for more in-depth Magento guides. If you have any further questions, leave us a comment.

At Magezon, we also provide you with many other fast, well-coded, yet affordable extensions for your store optimization. Visit our website to opt for the necessary ones!

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to build or optimize your Magento stores for sales-boosting? Then visit the Magezon website and grab the necessary add-ons for yours today!

About Hồng Hoàng

Hồng Hoàng

Check Also

How to Configure Magento 2 Persistent Shopping Cart

If you land here, you must find the most straightforward guide to configuring Magento 2 …

Leave a Reply