Jump to content

Removing Item From Cart


arunpatal

Recommended Posts

Hello everyone,

 

I have a cart file in which i want to make a remove button, which is display beside every items.

 

Here is the code for cart.php

 

 

<?php
session_start();

ini_set('display_errors', 1);
error_reporting(0);

require_once 'settings/config.php';


define("PRODUCTCODE", 0);
define("PRODUCTNAME", 1);
define("QUANTITY", 2);
define("PRICE", 3);



if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if (isset($_POST['productcode']))
  {
  AddToCart();
  }
  else
  {
  $action = isset($_POST['action']) ? $_POST['action'] : '';
  $value = trim($action);
  switch ($value)
  {
  // continue shopping
  case $continue_label:
	 header("Location: ".$shopping_url);
	 break;

  // recalculate
  case $recalculate_label:
	 RecalculateCart();
	 break;

  // proceed to checkout
  case $checkout_label:
	 header("Location: ".$cart_redirect);
	 break;
  }
  }
}


function AddToCart()
{
  $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
  $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

$productname = $_POST['productname'];
$extra_price = 0;
for ($opt = 1; $opt <= 8; $opt++){
$key = 'option_name'.$opt;
$key1 = 'option_values'.$opt;
$option_name = trim($_POST[$key]);
$option_value = trim($_POST[$key1]);

if ($option_value != "") {
unset($options);
$options = explode(",", $option_value);
$productname .= " / $option_name : ".$options[0];
$extra_price = $extra_price + $options[1];
}
}
$productname = stripslashes($productname);

// Let's see if this product already exists
  for ($i=0; $i < $itemcount; $i++)
  {
 if ($cart[PRODUCTNAME][$i] == $productname) {
	 $cart[QUANTITY][$i] = $cart[QUANTITY][$i] + intval($_POST['quantity']);
	 $_SESSION['cart'] = $cart;
	 $_SESSION['itemcount'] = $itemcount;
	 header("Location: "."cart.php");
	 exit;
  }
  }

$cart[PRODUCTCODE][$itemcount] = $_POST['productcode'];
$cart[PRODUCTNAME][$itemcount] = stripslashes($productname);
$cart[QUANTITY][$itemcount] = intval($_POST['quantity']);
$cart[PRICE][$itemcount] = $_POST['price'] + $extra_price;

  $itemcount = $itemcount + 1;

  $_SESSION['cart'] = $cart;
  $_SESSION['itemcount'] = $itemcount;
}

function RecalculateCart()
{
  include 'settings/config.php';

  $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
  $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

  for ($i=0; $i<$itemcount; $i++)
  {
  $quantity = $_POST['quantity'.($i)];
  if (empty($quantity))
  {
	 $quantity = 0;
  }
  else
  if (($quantity <= 0) || (!is_numeric($quantity)))
  {
	 $quantity = 0;
  }
  $cart[QUANTITY][$i] = intval($quantity);
  }

  for ($j=0; $j<$itemcount; $j++)
  {
  $quantity = $cart[QUANTITY][$j];

  // remove item from the cart
  if ($quantity == 0)
  {
	 $itemcount--;

	 $curitem = $j;

	 while(($curitem+1) < count($cart[0]))
	 {
	    for ($k=0; $k<7; $k++)
	    {
		   $cart[$k][$curitem] = $cart[$k][$curitem+1];
		   $cart[$k][$curitem+1] = '';
	    }
	    $curitem++;
	 }
  }
  }
  $_SESSION['itemcount'] = $itemcount;
  $_SESSION['cart'] = $cart;
 }
?>


<?php
include 'style/css_cookie_check.php'; // Check if the user has a style cookie set and access its value
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Shopping Cart</title>
<link href="style/<?php echo $styleChoice; ?>/style.css" rel="stylesheet" type="text/css" />

<meta name="author">


<script type="text/javascript">
<!--
function popupwnd(url, toolbar, menubar, locationbar, resize, scrollbars, statusbar, left, top, width, height)
{
  if (left == -1)
  {
  left = (screen.width/2)-(width/2);
  }
  if (top == -1)
  {
  top = (screen.height/2)-(height/2);
  }
  var popupwindow = this.open(url, '', 'toolbar=' + toolbar + ',menubar=' + menubar + ',location=' + locationbar + ',scrollbars=' + scrollbars + ',resizable=' + resize + ',status=' + statusbar + ',left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
}
//-->
</script>
<!--[if lt IE 7]>
<style type="text/css">
  img { behavior: url("pngfix.htc"); }
</style>
<![endif]-->
<script type="text/javascript">
<!--

function recalculate(sel)
{
var name = sel.name;
var location = 'cart.php?ship_to_country='+sel.options[sel.selectedIndex].value;
window.location = location;

}
//-->
</script>
</head>
<body>

<div align="center" id="mainWrapper">
 <?php include_once("header.php");?></div>

<table id="cart_page_maintable">
<td>



<div>
<form name="frmCart" method="post" action="cart.php" accept-charset="UTF-8" id="Form2">
<input type="hidden" name="resident" value="No">








<!-- Shopping Cart contents -->
<div>
<?php

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

$strHTML = "";

if ($itemcount == 0)
{
  $strHTML = "<h3>".$empty_cart_label."</h3>";
}
else
{
  $strHTML = '<div style="overflow:auto; height:100%;">';
  $strHTML .= '<table id="cart_product_disply_table">';
  $strHTML .= '<tr>';
  $strHTML .= '<td align="center" bgcolor="'.$TITLE_BCGRD_CLR.'"><font style="font-size:'.$TITLE_TEXT_SIZE.'px" color="'.$TITLE_TEXT_CLR.'" face="'.$TITLE_TEXT_FONT.'"><'.$TITLE_TEXT_STYLE.'>'.$product_code_label.'</'.$TITLE_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="center" bgcolor="'.$TITLE_BCGRD_CLR.'"><font style="font-size:'.$TITLE_TEXT_SIZE.'px" color="'.$TITLE_TEXT_CLR.'" face="'.$TITLE_TEXT_FONT.'"><'.$TITLE_TEXT_STYLE.'>'.$product_description_label.'</'.$TITLE_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="center" bgcolor="'.$TITLE_BCGRD_CLR.'"><font style="font-size:'.$TITLE_TEXT_SIZE.'px" color="'.$TITLE_TEXT_CLR.'" face="'.$TITLE_TEXT_FONT.'"><'.$TITLE_TEXT_STYLE.'>'.$product_qty_label.'</'.$TITLE_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="center" bgcolor="'.$TITLE_BCGRD_CLR.'"><font style="font-size:'.$TITLE_TEXT_SIZE.'px" color="'.$TITLE_TEXT_CLR.'" face="'.$TITLE_TEXT_FONT.'"><'.$TITLE_TEXT_STYLE.'>'.$product_price_label.' '.$currency_symbol.'</'.$TITLE_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="center" bgcolor="'.$TITLE_BCGRD_CLR.'"><font style="font-size:'.$TITLE_TEXT_SIZE.'px" color="'.$TITLE_TEXT_CLR.'" face="'.$TITLE_TEXT_FONT.'"><'.$TITLE_TEXT_STYLE.'>'.$product_amount_label.' '.$currency_symbol.'</'.$TITLE_TEXT_STYLE.'></font></td>';

  $subtotal = 0;
  $discount = $_SESSION['discount'];
  $total = 0;

  //$ship_to_country = isset($ship_to_country)? $ship_to_country : str_replace(" ", "_", $origin_country); // it sets the area to National if it is not defined
  $ship_area = isset($ship_to_country)? $area[str_replace(" ", "_", $ship_to_country)]:'0'; // it sets the area to National if it is not defined
  for ($i=0; $i<$itemcount; $i++)
  {
  $BGCOLOR=$BGCOLOR1;
  if ($i % 2 == 0) {
  $BGCOLOR=$BGCOLOR2;
  }

  $strHTML .= '<tr>';
  $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.$cart[PRODUCTCODE][$i].'</'.$CELL_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="left" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.$cart[PRODUCTNAME][$i].'</'.$CELL_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><input type="text" name="quantity'.($i).'" style="text-align:right;width:40px;font-family:'.$CELL_TEXT_FONT.';font-size:'.$CELL_TEXT_SIZE.'px" align="right" value="'.$cart[QUANTITY][$i].'" size="1"></td>';
  $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.number_format($cart[PRICE][$i],2).'</'.$CELL_TEXT_STYLE.'></font></td>';
  $strHTML .= '<td align="right"  bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.number_format($cart[PRICE][$i]*$cart[QUANTITY][$i],2).'</'.$CELL_TEXT_STYLE.'></font></td>';
  $strHTML .= '</tr>';


  $subtotal = $subtotal + ($cart[PRICE][$i]*$cart[QUANTITY][$i]);
     $total_weight += $cart[WEIGHT][$i]*$cart[QUANTITY][$i];
  }

$grandtotal = $nettotal;
$_SESSION['subtotal'] = $subtotal;
$_SESSION['discount'] = $discount;
$_SESSION['nettotal'] = $nettotal;

// Display Subtotal
if($discount > 0 or $taxes > 0 or $shipping > 0){
$strHTML .= '<tr>';
$strHTML .= '<td></td><td></td>';
$strHTML .= '<td colspan="2" align="left" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.$subtotal_label.', '.$currency_symbol.':</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '<td align="right" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.number_format($subtotal, 2).'</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '</tr>';
}
{

// Display Nettotal
$strHTML .= '<tr>';
$strHTML .= '<td></td><td></td>';
$strHTML .= '<td colspan="2" align="left" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.$nettotal_label.', '.$currency_symbol.':</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '<td align="right" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.number_format($nettotal, 2).'</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '</tr>';
}



// Display Grandtotal
$strHTML .= '<td></td><td></td>';
$strHTML .= '<td colspan="2" align="left" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.$grandtotal_label.', '.$currency_symbol.':</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '<td align="right" bgcolor="'.$TOTALS_BCGRD_CLR.'"><font style="font-size:'.$TOTALS_TEXT_SIZE.'px" color="'.$TOTALS_TEXT_CLR.'" face="'.$TOTALS_TEXT_FONT.'"><'.$TOTALS_TEXT_STYLE.'>'.number_format($grandtotal, 2).'</'.$TOTALS_TEXT_STYLE.'></font></td>';
$strHTML .= '</tr>';
$strHTML .= '</table>';
$strHTML .= '</div>';
}

echo $strHTML;


?>
</div>

<br />

<div id="cart_buttons">
<input type="submit" id="Button1" name="action" value="Recalculate">
<input type="submit" id="Button4" name="action" value="Check out" >
<input type="submit" id="Button2" name="action" value="Continue Shopping">
</div>

<br />

<div align="right">
<span>Please give your discount coupon</span> <input type="text" id="Editbox1" name="promo_code" value="<?php echo stripslashes($_SESSION['promo_code']);?>">
</div

><div> <select name="ship_to_country" size="1" id="country_list" onchange="recalculate(this);return false;"><?php
$countries = array_keys($area);
echo '<option value="">Please select</option>
';
for ($i = 0; $i < count($countries); $i++){
if ($area[$countries[$i]] != "" and $area[$countries[$i]] != "-") {
$countries[$i] = str_replace("_", " ", $countries[$i]);
echo '<option ';
if ($shiptocountry == $countries[$i]) {
echo 'selected ';
}
echo 'value="'.$countries[$i].'">'.$countries[$i].'</option>
';
}
}
?>
</select>

<?php
echo'<span style="color:#A52A00;font-family:Arial;font-size:12px;">Shipping costs are calculated for '.$shiptocountry.'.</span>';
?></div>
</form>
</div>

</td>
</table>


<div>
 <?php include_once("footer.php");?>
 </div>



</body>
</html>

Edited by arunpatal
Link to comment
Share on other sites

What part are you having trouble with?

 

If you see here.....

 

$strHTML .= '<tr>';
	 $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.$cart[PRODUCTCODE][$i].'</'.$CELL_TEXT_STYLE.'></font></td>';
	 $strHTML .= '<td align="left" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.$cart[PRODUCTNAME][$i].'</'.$CELL_TEXT_STYLE.'></font></td>';
	 $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><input type="text" name="quantity'.($i).'" style="text-align:right;width:40px;font-family:'.$CELL_TEXT_FONT.';font-size:'.$CELL_TEXT_SIZE.'px" align="right" value="'.$cart[QUANTITY][$i].'" size="1"></td>';
	 $strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.number_format($cart[PRICE][$i],2).'</'.$CELL_TEXT_STYLE.'></font></td>';
	 $strHTML .= '<td align="right" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.number_format($cart[PRICE][$i]*$cart[QUANTITY][$i],2).'</'.$CELL_TEXT_STYLE.'></font></td>';
	 $strHTML .= '</tr>';

 

This is the display table for showing product details.....

 

 

Now this code remove product from cart when the quantity set to zero and recalculate button is pressed

 

if ($quantity == 0)
	 {
			 $itemcount--;

			 $curitem = $j;

			 while(($curitem+1) < count($cart[0]))
			 {
				 for ($k=0; $k<7; $k++)
				 {
					 $cart[$k][$curitem] = $cart[$k][$curitem+1];
					 $cart[$k][$curitem+1] = '';
				 }
				 $curitem++;
			 }
	 }

 

now i add one extra button with each product code like this

$strHTML .= '<td align="center" bgcolor="'.$BGCOLOR.'"><font style="font-size:'.$CELL_TEXT_SIZE.'px" color="'.$CELL_TEXT_CLR.'" face="'.$CELL_TEXT_FONT.'"><'.$CELL_TEXT_STYLE.'>'.$cart[PRODUCTCODE][$i].'

<input name="deleteBtn' .$cart[PRODUCTCODE]. '" type="submit" value="remove" />

</'.$CELL_TEXT_STYLE.'></font></td>';

 

 

now i want to make isset statement or maybe other for this input field

 

that if someone click this remove button then that item will be removed.....

Edited by arunpatal
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.