Disable shopping cart saving on customer logout in OpenCart 2.0.x

When you have an account in a OpenCart store and add product(s) in the shopping cart, the cart contents are preserved in the database. That way if you log out and then log in again after some time, the cart contents will be saved and still available for you. However, some customers do prefer the shopping cart data to stay only in the current session and not be saved in the database. In this post we will show you how to achieve exactly that.

What you have to do?

Modifying core files is not recommended and because of that we will use the integrated OCMod modification system in OpenCart 2.0.x.

First of all, you need to create a new file with the extension *.ocmod.xml (e.g. somename.ocmod.xml). Make sure that the end of the name of the file ends with *.ocmod.xml, so it could be recognized by the Extension Installer presented in OpenCart.

When you are ready with the first step, paste the code below in the new file:

<modification>
    <name>Disable Shopping Cart Save by iSenseLabs</name>
    <version>1.0 (Initial)</version>
    <link>https://isenselabs.com</link>
    <author>iSenseLabs</author>
    <code>isenselabs_disablecart</code>
  
    <file path="system/library/customer.php">
        <operation>
            <search><![CDATA[$this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "' WHERE customer_id = '" . (int)$this->customer_id . "'");]]></search>
            <add position="replace"><![CDATA[
            $this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
            ]]></add>
        </operation>
    </file>
  
      <file path="catalog/language/english/account/logout.php">
        <operation>
            <search><![CDATA[$_['text_message']  = '<p>You have been logged off your account. It is now safe to leave the computer.</p><p>Your shopping cart has been saved, the items inside it will be restored whenever you log back into your account.</p>';]]></search>
            <add position="replace"><![CDATA[
            $_['text_message']  = '<p>You have been logged off your account and your shopping cart is cleared. It is now safe to leave the computer.</p>';
            ]]></add>
        </operation>
    </file>
</modification>

You might notice that this modification is hard-coded for stores using the English language. If you are using another language, make sure to update the modification file on line

<file path="catalog/language/english/account/logout.php">

By changing ‘english’ to the according folder. You should also translate the phrase on the line starting with $_['text_message'] to your preference.

When you save the file, to your Administration Page > Extensions > Extension Installer and upload the file. You should get the message 'Success: You have installed your extension'. This means that the modification file was uploaded successfully and all you have to now is apply it. For that to happen, go to Extensions > Modifications and click on the “Refresh” button.
That’s it!

I hope that you will find this article useful and try the modification. If you have any questions, feel free to ask them in the comments section below.

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time

Trending blogs

comments powered by Disqus