This article describes how to remove individual products and entire product lines from a shopping cart by using AJAX requests.
The product line in a shopping cart must have an element that can be clicked to initialize the removal script. In this example, we use the default output of the {Cart} tag, in which the element can be found based on the a.CartRemove identifier.
Below you can see the example of a simplified shopping cart page implementation:
<div class="CartContainer">
{Cart}
</div>A product is removed from the shopping cart by sending a POST request to the address /cart/delete/n where n is the ID of the product to be deleted.
/*
* Suoritetaan poistoskripti a.CartRemove-elementin klikkauksella
*/
$("a.CartRemove").on("click", function(event) {
event.preventDefault();
/*
* Otetaan linkin osoitteesta talteen poistettavan
* tuotteen ID.
*/
productId = $(this).attr("href").split("/")[3];
/*
* Lähetetään POST-pyyntö
*/
$.ajax({
type: "POST",
url: "/cart/delete/" + productId,
success: function() {
/*
* Nyt tuote on poistettu, joten ladataan ostoskori uudestaan, että
* muutos saadaan asiakkaan näkyville.
*/
$.ajax({
type: "GET",
url: "/interface/Cart",
success: function(data) {
$(".CartContainer").html(data);
}
});
}
});
});You can implement multiple functions connected to purchasing by using asynchronous JavaScript calls. Read also the following articles about asynchronous actions:
Removing products with the default theme's Cart plugin
In MyCashflow's default theme, removing products from the shopping cart has already been implemented by using an AJAX function.
