You can clear the online store's cart in the theme in two ways: using a form or an asynchronous JavaScript call.
Introduction
In each of the examples below, a form is created on the page to empty the shopping cart:
<form id="cartEmptyForm" action="/cart/empty" method="POST">
<button>{%ClearCart}</button>
</form>You can add the form, for example, to any page that displays the cart or parts of it. A good practice is to display some kind of preview of the shopping cart on every page (e.g., with a {MiniCart} tag) and provide a more detailed presentation on the shopping cart page.
The JavaScript examples in this article use jQuery library functions. If you use the scripts described in this article in your own theme, make sure that jQuery has been added to the page.
You can add jQuery to the theme by using the {SupportScripts} tag.
Emptying the shopping cart with a form
When you add the attribute action="/cart/empty" to the form, the shopping cart is emptied when the form is submitted. The form does not need any fields other than a submit button, i.e. the <button type="submit"> element.
Emptying the shopping cart with JavaScript
If you'd like to reduce the number of page reloads in your online store, you can implement an AJAX call for emptying the shopping cart.
In the code below, the shopping cart is emptied and the element that contains the {MiniCart} tag is reloaded:
function emptyAndReloadCart() {
$.ajax({
type: "POST",
url: "/cart/empty",
data: "", // data-attribuutti voidaan jättää tyhjäksi tai kokonaan pois
success: function() {
/* Nyt ostoskori on tyhjä
*
* Tyhjennetään elementti, joka sisältää MiniCart-tagin
*/
$("#MiniCartContainer").empty();
// Haetaan MiniCart asynkronisesti ja kirjoitetaan se aiemmalle paikalleen
$.get('/interface/MiniCart', function (response) {
$("#MiniCartContainer").html(response);
});
}
});
// Sidotaan ostoskorin tyhjennysfunktio lomakkeen lähetyspainikkeeseen
$.on("click", "#cartEmptyForm>button", function(event) {
event.preventDefault();
emptyAndReloadCart();
});If the form for emptying the shopping cart is used on the shopping cart page, you may want to reload the contents of the shopping cart asynchronously so that the customer can see the change without page reload.
You can implement multiple functions connected to purchasing by using asynchronous JavaScript calls. Read also the following articles about asynchronous actions:
