Creating a “Clear cart” button in PrestaShop

In this tutorial, we will see how to add a button to empty the PrestaShop Cart in one click.

Adding the button to the cart page

The first thing we need is, of course, a button to play with. The ideal place where to add it is shopping-cart.tpl, specifically right after the order summary table. THerefore, open un the file, located in the theme folder, and read the end of the table, around line 454 of the default template:


Right after the table, let’s add a simple button:

		<a class="btn btn-default pull-right" id="removeAll" href="javascript:void(0)">
			Empty Cart

Then, we need it to do something. We could use a submit, inside a form, but modern standards require speed, and nothing beats ajax in this.
Right after the button, let’s add a script tag:

			$(document).ready(function() {
				$('#removeAll').click(function(e) {

We want to target the click event of our new button, and fire an ajax request to the cart controller:

$(document).ready(function() {
	$('#removeAll').click(function(e) {
			type: 'POST',
			headers: { "cache-control": "no-cache" },
			url: baseUri + '?rand=' + new Date().getTime(),
			async: true,
			cache: false,
			dataType : "json",
			data: 'controller=cart&deleteAll=1&token=' + static_token + '&ajax=true',
			success: function(data){
				$('.opc-main-block, .step-num, #HOOK_SHOPPING_CART_EXTRA').fadeOut('fast');
				$('#order-detail-content').fadeOut('fast', function() {

Explanation: The url we call is simply the base one, plus a random value with the date, to avoid the request being cached. In terms of data, we need to pass in the controller (cart), and action (deleteAll, which doesn’t exist yet, but we will create in a second), the security token, and then ajax=true.
On success, we fade out the page content, making sure any stuff from the One Page Checkout is hidden as well, and we also refresh the cart block.

The next step is to create something to handle the new deleteAll command.

Editing the CartController

As always, instead of modifying the core file, make sure you use an override for this method. The function we are interested in is postProcess:

    public function postProcess()
        // Update the cart ONLY if $this->cookies are available, in order to avoid ghost carts created by bots
        if ($this->context->cookie->exists() && !$this->errors && !($this->context->customer->isLogged() && !$this->isTokenValid())) {
            if (Tools::getIsset('add') || Tools::getIsset('update')) {
            } elseif (Tools::getIsset('delete')) {
            } elseif (Tools::getIsset('changeAddressDelivery')) {
            } elseif (Tools::getIsset('allowSeperatedPackage')) {
            } elseif (Tools::getIsset('duplicate')) {
            // Make redirection
            if (!$this->errors && !$this->ajax) {
                $queryString = Tools::safeOutput(Tools::getValue('query', null));
                if ($queryString && !Configuration::get('PS_CART_REDIRECT')) {

                // Redirect to previous page
                if (isset($_SERVER['HTTP_REFERER'])) {
                    preg_match('!http(s?)://(.*)/(.*)!', $_SERVER['HTTP_REFERER'], $regs);
                    if (isset($regs[3]) && !Configuration::get('PS_CART_REDIRECT')) {
                        $url = preg_replace('/(\?)+content_only=1/', '', $_SERVER['HTTP_REFERER']);

                Tools::redirect('index.php?controller=order&'.(isset($this->id_product) ? 'ipa='.$this->id_product : ''));
        } elseif (!$this->isTokenValid()) {

See all those elseifs? We need another condition to target our deleteAll command. Therefore, at the end of the stack, add another:

} elseif (Tools::getIsset('duplicate')) {
} elseif (Tools::getIsset('deleteAll')) {

// Make redirection
if (!$this->errors && !$this->ajax) {

Inside it, we do not need fancy stuff, only:

elseif (Tools::getIsset('deleteAll')) {
    $this->context->cookie->id_cart = 0;


This will clear the cart for good, and reset the id for the current user. You can also avoid resetting it, if you prefer.
Save and test the button now. Make sure you clear the class_index.php file inside cache/, if you used an override.
We are done!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Fran

    Hello, can you include the entire code postprocess() function?
    That is, all together, to be split does not work me, I might be doing something wrong.

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: