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
If you want to override the navigation-menu.js you need to add it in your theme:
app/design/frontend/{yourVendorName}/{yourThemeName}/web/js/navigation-menu.js
After that you need to run:
php bin/magento setup:static-content:deploy
and clear the cache, just to be sure.
I hope this helps!
Best Answer
Magento provides a facility to persist configuration values like tokens, passwords, URLs etc. This is the System (Store) Configuration (Menu > Stores > Configuration).
See the
/etc
section of this documentation onsystem.xml
and other configuration files: http://devdocs.magento.com/guides/v2.1/architecture/archi_perspectives/components/modules/mod_anatomy.htmlSo, in your module directory
etc/adminhtml/system.xml
will hold the XML which will create your configuration.You have an option to create a new section for your configuration or to add your configuration to an already existing area. You can also create a whole new tab, but this is typically not a good idea, as it tends to clutter up the top navigation. You can most-likely find a good spot in the existing configuration for your settings.
There's quite a bit of stuff in there, but the main choices you need to make are the scope (See more on that here: http://docs.magento.com/m2/ee/user_guide/configuration/scope.html) and the type of data you are storing.
Scope means whether or not there is a single global value, or the user can set a different value for different website, store and store view. In your case, you probably want to allow the user to have a different token for a different website, but not store views.
Data types will typically be "text" for your use case, but when stored it may also be encrypted in case you consider this to be something that users should be able to only set, but not read once it has been saved. (You will be able to decrypt the value through code.)
ACL If you are creating a new tab (as in the example below), you should also define an ACL resource, so that you may allow or prohibit access for only some User Roles.
You can find many examples of
system.xml
,acl.xml
andconfig.xml
in the Magento core modules, so you can take a look at those and fashion your own.Example So, let's say your module integrates with an external service and you consider the token to be secret. Your configuration should probably live in the Services Menu, the scope will be available globally but can be overridden with different values for each website. The data type will be Encrypted.
etc/adminhtml/system.xml
etc/adminhtml/acl.xml
config.xml You need to add a few nodes in your config.xml in order for Magento to automatically decrypt your value when read from the database.
Now, you can use this in your code.
In your class constructor inject the config model
NOTES
\Magento\Framework\App\Config\ScopeConfigInterface::getValue()
method. If you wanted to make this more robust you would provide the scope ID as a method parameter to your methodgetToken
and then pass it togetValue
as the third parameter. Of course, you would need to know which scope ID to use. Typically this is done by pulling the website ID or store ID from some other object.