0

The problem is even when i disable the "persistent checkout", it still work as normal.
Its a magento default module, so i think this is a magento 2 bug. enter image description here

After a day working on this, i still don't know how this work. I mean i guess they use cookie or session to store the cart after customer log out.
Then after guest login, they will use that session or cookie to update the cart.
But i don't know how they do that.

The module is "module-persistent" (default magento module).
Thanks.

  • have you clean the configuration cache? – Philipp Sander Jan 21 at 14:24
  • Yes i did serveral time, as i said, i was working on this all day long, so the first thing i have thinking of is "clear cache" :D – fudu Jan 21 at 14:34
0

Well, so after a day looking for solution, i've found something.
But this is only a temporary solution.
So here is the idea:
We override the magento/customer/account/logout by plugin, then we will truncate the cart.

di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\Logout">
        <plugin name="remove_cart_before_logout" type="Fudu\Customer\Plugin\Controller\Account\Logout" sortOrder="1" />
    </type>    
</config>

Plugin/Controller/Account/Logout

<?php 
namespace Fudu\Customer\Plugin\Controller\Account;

use Magento\Customer\Controller\Account\Logout as CustomerLogout;

class Logout
{
    public function __construct(
        \Magento\Checkout\Model\Session $session,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Persistent\Helper\Data $persistentData
    ){
        $this->_session = $session;
        $this->cart = $cart;
        $this->persistentData = $persistentData;
    }

    public function beforeExecute(CustomerLogout $subject)
    {
        if(!$this->persistentData->isEnabled()){
            $this->cart->truncate()->save();
        }
    }
}

Also, we have to override this file too, in case customer doesn't logout, they close the tab and open a private browser:

di.xml

<?xml version="1.0"?>
<config>
    <preference for="Magento\Checkout\Model\Session" type="Gssi\Checkout\Model\Session" />
</config>

Magento\Checkout\Model\Session.php

public function loadCustomerQuote()
    {
        if (!$this->_customerSession->getCustomerId()) {
            return $this;
        }

        // put this in, and everything is still the same.
        if(!$this->persistentData->isEnabled()){
            return $this;
        }

        $this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);

        try {
            $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $customerQuote = $this->quoteFactory->create();
        }
        $customerQuote->setStoreId($this->_storeManager->getStore()->getId());

        if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
            if ($this->getQuoteId()) {
                $this->quoteRepository->save(
                    $customerQuote->merge($this->getQuote())->collectTotals()
                );
            }

            $this->setQuoteId($customerQuote->getId());

            if ($this->_quote) {
                $this->quoteRepository->delete($this->_quote);
            }
            $this->_quote = $customerQuote;
        } else {
            $this->getQuote()->getBillingAddress();
            $this->getQuote()->getShippingAddress();
            $this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
                ->setTotalsCollectedFlag(false)
                ->collectTotals();
            $this->quoteRepository->save($this->getQuote());
        }
        return $this;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.