Please make change in following setting. Go to following setting in admin:
Stores > Configuration > Customers > Persistent Shopping Cart
Persist Shopping Cart set to No
If still it'll not work. Make small module like following.
To remove Cart item when customer logout. You can make small module for this functionality. The code is verified and it is working in Magento 2.1.
app/code/Rohit/UserCart/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Rohit_UserCart',
__DIR__
);
app/code/Rohit/UserCart/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Rohit_UserCart" setup_version="1.0.0" >
</module>
</config>
app/code/Rohit/UserCart/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_logout">
<observer name="clearCartOnLogout" instance="Rohit\UserCart\Observer\EmptyCartLogout" />
</event>
</config>
app/code/Rohit/UserCart/Observer/EmptyCartLogout.php
<?php
namespace Rohit\UserCart\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Checkout\Model\Cart as CustomerCart;
/**
* Class EmptyCartLogout
* @package Rohit_UserCart
*/
class EmptyCartLogout implements ObserverInterface
{
/**
* @var CustomerCart
*/
protected $cart;
/**
* EmptyCartLogout constructor.
* @param CustomerCart $cart
*/
public function __construct(
CustomerCart $cart
){
$this->cart = $cart;
}
/**
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->cart->truncate()->save();
}
}
After that run following command at your magento root
php bin/magento module:enable Rohit_UserCart
php bin/magento setup:upgrade
Hope this will helpful to you
That's how I thought it should work as well (see also: What does "Persistent Shopping Cart" do?)
But after inspecting the code, you are right, the persistent_shopping_cart
cookie is only set on login and logout, and used for the "remember me" feature to automatically log in customers.
So the feature does not quite live up to its promises and I am not sure if this is intentional or a bug.
Best Answer
@Marius while you would expect that to be logical in reality there is an edge case where the guest checkout option is disabled.
Replication
var/session
directoryCause
An event called
checkout_allow_guest
is fired byMage_Checkout_Helper_Data::isAllowedGuestCheckout()
This then observed by Mage_Persistent_Model_Observer::disableGuestCheckout() which disables the option to use guest checkout.
The cause of which is that the session is loaded by Mage_Persistent_Helper_Session::isPersistent() with the value of the persistence cookie:
Fix - Disabled event observer
You will need to create a new module with two files.
app/code/local/JZah/AllowPersistentGuestCheckout/etc/config.xml
:app/etc/modules/JZah_AllowPersistentGuestCheckout.xml
:Created for Magento EE 1.14.1.0