want to minus the quantity when click delete if cart qty is more then 1

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])){
    $product_code   = $_GET["removep"]; //get the product code to remove
    $product_s      = $_GET["removes"]; //get the product size to remove
    $return_url     = base64_decode($_GET["return_url"]); //get return url

    foreach ($_SESSION["products"] as $key=>$cart_itm){ //loop through session array var
        if($cart_itm["id"] !=$product_code || $cart_itm["size"] !=$product_s){ //item does,t exist in the list
          $product[] =array(

              'name'=>$cart_itm["name"],
              'id'=>$cart_itm["id"], 
              'size'=>$cart_itm["size"],
              'qty'=>$cart_itm["qty"],
              'price'=>$cart_itm["price"]);
            }
        //create a new product list for cart
         $_SESSION["products"] = $product;
    }
        //redirect back to original page
    header('Location:'.$return_url);
}

this code is deleting the product but i want minus also when qty is greater then 1

Recommended Answers

All 3 Replies

Why don't you just select the product merely by the product code? Or isn't that unique?

E.g.

foreach($_SESSION['products'] as $key => $cart_item) {
    if($product_code == $_GET['product_code']) {
        //* This is the product of which we're reducing the quantity.

        // Substract one (or whatever you want) from the quantity.
        $cart_item['quantity'] -= 1;

        // Save the product in its session variable.
        $_SESSION['products'][$key] = $cart_item;
    }
}

its not working in my code

You should not use that exact example, but modify it to your own needs. Or did you do that already and is it still not working?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.