magefan / module-login-as-customer

Allows admin to login as a customer (enter to customer account).
https://magefan.com/login-as-customer-magento-2-extension
Other
106 stars 32 forks source link

Cart items duplicated on another customer cart #5

Closed gamerboy closed 7 years ago

gamerboy commented 7 years ago

Hi,

i'm logged as Andrea, and i have ten items in my shopping cart. From Magento backend, i use the "login as customer" option on user Mario. Mario's cart is filled with Andrea cart items. Do you have any suggestion?

Magento EE: 2.1.5 Magneto 2 Login as Customer: 2.1.1

Best regards, Andrea.

magefan commented 7 years ago

@gamerboy, thank your for your report. Please try this fix https://github.com/magefan/module-login-as-customer/commit/f5aa305af16fa5893b99e56be96d596bbcdc4a92. Let us know if it work for you.

gamerboy commented 7 years ago

It works on my local pc. I will try it asap on production server and let you know. Thank you @magefan .

pradeep38mail commented 5 years ago

It clears the current logged user cart as well. It's not proper solution.

magefan commented 5 years ago

@pradeep38mail , do you use the latest extension version 2.1.9? It should clear the cart only when you were logged out (a guest) and this is correct way extension should work.

When you are a customer (CustomerA) before "login as a customer" from admin panel and have some products in your cart. Then when you login as a customerB, you are just logged out from CustomerA. Then when you logged out from customerB and log back to A.you will have the same cart items.

pradeep38mail commented 5 years ago

It's not like that, the fix you have applied will always make the user logged out before reaching it to the Clear cart code in Login.php and it always clears the current cart. Try to test that.

magefan commented 5 years ago

@pradeep38mail , I do not agree with you. This code


            $session = $this->_objectManager
                ->create('\Magento\Customer\Model\Session');
            if ($session->getId()) {
                /* Logout if logged in */
                $session->logout();
            } else {
                /* Remove items from cart */
                $cart = $this->_objectManager
                    ->create('\Magento\Checkout\Model\Cart');
                foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
                    $cart->removeItem($item->getId());
                }
                $cart->save();
            }

do next

If a customer is logged in
    then logout it
else 
   remove all items from the cart

So it removes items from the cart only if browsed storefront as a guest. Also, I recommend you to check the current extension version files https://github.com/magefan/module-login-as-customer/blob/master/Controller/Login/Index.php https://github.com/magefan/module-login-as-customer/blob/master/Model/Login.php

You will see that we have the option to keep Items in cart, and remove items only if (!$keepItems) {

and this option is managed from extension settings in the admin panel.

pradeep38mail commented 5 years ago

It's like this. The user is being logged out $session = $this->_objectManager ->create('\Magento\Customer\Model\Session'); if ($session->getId()) { / Logout if logged in / $session->logout(); } else { / Remove items from cart / $cart = $this->_objectManager ->create('\Magento\Checkout\Model\Cart'); foreach ($cart->getQuote()->getAllVisibleItems() as $item) { $cart->removeItem($item->getId()); } $cart->save(); } then, when it goes to the https://github.com/magefan/module-login-as-customer/blob/master/Model/Login.php The user is already logged out and it will be treated as guest cart. So it's removing the current cart.

magefan commented 5 years ago

@pradeep38mail , Let's talk about resent code \Magefan\LoginAsCustomer\Controller\Login\Index::execute call \Magefan\LoginAsCustomer\Model\Login::authenticateCustomer() in authenticateCustomer if a customer is logged in, then it will be logged out ELSE IF a guest and enabled items removal, items will be removed from cart.

There is no chance that items will be removed from the customer quote.