Jump to content

Shopping Cart


jay_bo

Recommended Posts

I have created a shopping cart and the products that have been added to the cart get displayed in the shopping cart page using this code, which works fine.

 

foreach($_SESSION['product'] as $prod)

{

if ($prod["qty"]!=0)

{

echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<br/>';

}

}

 

and i add the products for the products page by

 

if ($_GET["add"]=="true")//This line gets the value and reads it

{

$_SESSION["cost"]=($_SESSION["cost"]+$_POST["price"]*$_POST["quanity"]);

$_SESSION["products"]=$_SESSION["products"]+$_POST["quanity"];

$_SESSION["order"]=$_SESSION["order"].$_POST["id"].",";

$_SESSION["product"][$_POST["id"]]["qty"]=$_SESSION["product"][$_POST["id"]]["qty"]+$_POST["quanity"];

header ('Location: products.php?cat=1');

}

 

How would i be able to delete a product from my shopping cart page?

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/192060-shopping-cart/
Share on other sites

i'm sorry i just got what you actually ment.....

 

im displaying the products with this code and a link to remove each one.....

foreach($_SESSION['product'] as $prod)

{

if ($prod["qty"]!=0)

{

echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?delete=true">Remove</a>'.'<br/>';

}

}

 

Then my remove code is....

if ($_GET["delete"]=="true")//This line gets the value and reads it

{

unset($prod["qty"]);

header ('Location: products.php?cat=1');

}

Link to comment
https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012262
Share on other sites

You will want to send the product id in the link too so you delete the correct $_SESSION variable. I'd change this

foreach($_SESSION['product'] as $prod)
      {
         if ($prod["qty"]!=0)
         {
            echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?delete=true">Remove</a>'.'<br/>';
         }
      }

to

foreach($_SESSION['product'] as $prod_id => $prod)
      {
         if ($prod["qty"]!=0)
         {
            echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?id='.$prod_id.'&delete=true">Remove</a>'.'<br/>';
         }
      }

 

Now when deleting the product use

if (isset($_GET['id'], $_GET["delete"]) && is_numeric($_GET['id']) && $_GET['delete'] =="true")//This line gets the value and reads it
{
   $prod_id = (int) $_GET['id'];
   unset($_SESSION["product"][$prod_id]);
   header ('Location: products.php?cat=1');
}

Link to comment
https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012264
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.