PHP Shopping Cart – Update/Delete Product from Cart

In this tutorial we will see how to update or delete product(s) from cart. Usually shopping cart has separate cart page before checkout. I will also make separate page for cart. As you know, in my last post I have implemented add to cart functionality using session. So in this tutorial I will be printing session data with some other actions.

php shopping cart update delete cart

PHP:

In above code I have used session_start function and include config.php and helpers.php file. And then I put if condition to remove cart item(s). After that I have included header.php

HTML:

As you can see after starting 2 divs. I have added a condition for empty cart. If session is empty or having no data in cart_items index, I will print empty cart message. Next I have added condition for not empty cart_items and display cart data using foreach loop. Each cart item has trash bin and quantity input field. User can remove or add quantity using trash and quantity input. In the end I am displaying total quantity and total price. There is also a clear cart button which will use to clear cart items.

jQuery:

In footer.php, I added cart.js file which has above code. First event is used to update cart quantity. Whenever user change the quantity of a product this event will trigger ajax request to ajax_calls.php and that script would update cart quantity. Second event is used to empty shopping cart. When user will click on Clear Cart button, this event will trigger.

Ajax Request:

This file has 2 separate conditions. First one is for update cart quantity in session. And second condition is used to clear all cart data in session.

Also read:

 

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *