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 up the file, located in the theme folder, and read the end of the table, around line 454 of the default template:
{/foreach}
</tbody>
{/if}
</table>
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 </a>
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:
<script>
$(document).ready(function() {
$('#removeAll').click(function(e) {
});
});
</script>
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) {
e.preventDefault()
$.ajax({
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() {
$('#emptyCartWarning').fadeIn('slow');
});
ajaxCart.refresh();
}
})
});
});
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')) {
$this->processChangeProductInCart();
} elseif (Tools::getIsset('delete')) {
$this->processDeleteProductInCart();
} elseif (Tools::getIsset('changeAddressDelivery')) {
$this->processChangeProductAddressDelivery();
} elseif (Tools::getIsset('allowSeperatedPackage')) {
$this->processAllowSeperatedPackage();
} elseif (Tools::getIsset('duplicate')) {
$this->processDuplicateProduct();
}
// Make redirection
if (!$this->errors && !$this->ajax) {
$queryString = Tools::safeOutput(Tools::getValue('query', null));
if ($queryString && !Configuration::get('PS_CART_REDIRECT')) {
Tools::redirect('index.php?controller=search&search='.$queryString);
}
// 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($url);
}
}
Tools::redirect('index.php?controller=order&'.(isset($this->id_product) ? 'ipa='.$this->id_product : ''));
}
} elseif (!$this->isTokenValid()) {
Tools::redirect('index.php');
}
}
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')) {
$this->processDuplicateProduct();
} 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->cart->delete();
$this->context->cookie->id_cart = 0;
die(1);
}
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!



