14. E – Commerce Website Tutorial – Remove Items From PHP Shopping Cart

buy amoxil online australia

Series: http://www.developphp.com/video/PHP#eCommerce-Website-Tutorial
Source Download: http://www.developphp.com/video/PHP/PayPal-IPN-PHP-Instant-Payment-Notification-Script
In this 14th video we will learn a simple method for allowing the user to remove cart items from your cart’s multidimensional array using unset() and sort(). The E – Commerce Web Site we will be assembling is one in which all of the inventory is in a MySQL database and we offer a custom PHP cart for a unique shopping experience.


25 thoughts on “14. E – Commerce Website Tutorial – Remove Items From PHP Shopping Cart”

  1. click here to find out more Adam Khoury…That great tuts over there. Thank you so much. Could there be a way where customers would know the number of items in stock (inventory) and how many left. Also a mechanism that wil make users know that these items are finished and so cant be purchased…Hope to hear from you sooo

  2. When I remove something from the cart it sorts the remaining items in the cart in alphabetical order not in the order they were put in originally so eg. just say you had purple jeans, black hat , black jeans and  you removed black hat. What you end up with is black jeans , purple jeans so the sort function changes the order of the items in the cart alphabetically by name rather than the order they were added in.
    That's how it works for me unfortunately. Am I doing something wrong?
    Anyone know a way of changing it so the order of items in the cart doesn't change like that? It just doesn't seem right to me.

  3. As I understand correctly, every time we press the Delete button there is a page reload happens. Why don't we use javascript/Ajax functionality instead to remove items from the cart?

  4.  Y ou have perhaps one of the best tutorials on youtube and yet, i'm still to see your follow up on all the questions from your subscribers in regards to tutorial 10- 14. Please kindly reply us as this will help us a long way. Thanks once again.

  5. <?php
    //section 1 (if the user attempts to add something to the cart from product page)
    $pid = $_POST['pid'];
    $wasFound= false;
    //if the cart session variable is not set or cart array is empty
    if(!isset($_SESSION["cart_array"])  ||  count($_SESSION["cart_array"]) < 1){
    // Run it the cart is empty or not set
    $_SESSION["cart_array"] = array(0 => array("item_id" =>$pid, "quantity" => 1));
    // Run if the cart has at least item in it
    foreach($_SESSION["cart_array"] as $each_item){
    while(list($key,$value) = each($each_item)){
    if($key == "item_id" && $value == $pid){
    //That item is in the cart already so lets adjust its quantity using array_splice()
    array_splice($_SESSION["cart_array"], $i-1,1,array(array("item_id" => $pid, "quantity" => $each_item['quantity']+1)));
    }//close if condition
    }//close while loop
    }//close foreach conditionn
    if($wasFound == false){
    array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    // section 2 (if the user chooses to their shopping cart)
    if(isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){
    //section 3(if user want to remove an item from cart)
    if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']!=""){
    //Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    //section 4 (render the cart for the user to view)
    $cartOutput = "";
    $cartTotal = "";
    if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
    foreach($_SESSION["cart_array"] as $each_item){

    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
    $product_name = $row["product_name"];
    $price = $row["price"];
    $details = $row["details"];
    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;
    //Dynamic table row assembly
    $cartOutput .= '<tr>';
    $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1"/></td>';
    $cartOutput .= '<td>'. $details . '</td>';
    $cartOutput .= '<td>$'. $price . '</td>';
    $cartOutput .= '<td>'. $each_item['quantity'] . '</td>';
    $cartOutput .= '<td>$'. $pricetotal . '</td>';
    $cartOutput .= '<td>' . $i . '<form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '"></form></td>';
    $cartOutput .= '</tr>';
    $cartTotal = "<div align='right'>Cart Total: $" .$cartTotal. " php</div>";
    whats wrong with my code???? :((((((

  6. Or even better you can use this:
    array_splice($_SESSION['cart_array'], $key_to_remove, 1);

    this time you don't even need to unset() element, before using array_splice

  7. Instead of using sort() it may be more sense to try array_values() function:
    $_SESSION['cart_array'] = array_values($_SESSION['cart_array']);
    In this case the items will be in the original order.

    And sort() function assigns keys to the items randomly. So everytime user deletes an item, he may see that items in his cart have changed their places.


  8. hey adam thanks alot……..
    bt the thing is that i m having three products on my page the problem is coming when i try 2 remove any products from my cart if i click on remove 1st product the third product gets deleted and again when i click on remove 1st product then the second product gets deleted…….and then the 1st product gets deleted……..and when there are 3 products i try to remove 3rd product and click on X nothing happens………pls adam help me

  9. it might just be cosmetic but can't you put the total in the more conspicuous place?

    and calculate if taxes are needed in the "state" the customer is in.

    and calculate the "shipping" cost?

  10. Adam,

    5 Star rating again for this tutorial. After I watch it several times I am sure it will sink in. This is all totally new information for me to understand.

    Thank you,


  11. thanks adam that tut was great my site is starting to come together now
    i have posted on the developphp site the code for a custom image as a button rather than the standard one if anyone wants it i am afraid it has all the in it still but i am sure if you want to change it you can

  12. Great Tut Adam, I just have a question, how come for the item amounts u arent using the way alot of site do where the item amount is a text box and where u have the delete button its an update button and if u put 0 in the text box it removes the item..im just wondering why u didnt go this route seem as it seems better and alot of sites do it so it must be good right? 😛

  13. i think you should put the cart total on the top of the cart table that way no matter how many items they have in the cart they don't have to scroll to the bottom to get it ratter they just go to the top

Leave a Reply

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