Nieuw lid |
|
Hallo iedereen,
Ik ben voor een vriend een website aan het bouwen (voor een broodjesbar waarbij men items in een winkelwagentje kan voegen en deze achteraf doormailen).
Alles werkt perfect behalve dat ik nu vastzit met 1 ergelijk probleem.
Het script update produkten in de winkelwagen aan de hand van hun item_id (denk ik) en verhoogt gewoon de prijs en het aantal.
Ik zou het graag zo zien gebeuren dat elk item dat besteld wordt, ook al bestaat het al in het mandje, dat deze weergegeven wordt op een nieuwe regel in de tabel als nieuw produkt.
vb)
SLECHT:
2 |broodjes kaas| €4,00
2 |broodjes hesp| €4,00
GOED:
1 |broodje kaas| €2,00
1 |broodje kaas| €2,00
1 |broodje hesp| €2,00
1 |broodje hesp| €2,00
Volgens mij is het maar een kleine aanpassing, maar ik zit momenteel muurvast, misschien dat hier iemand mij uit de nood kan helpen.
dank op voorhand.
<?php
class cart
{
var $items;
var $sub_total;
function cart() {
$this->items = array();
$sub_total = 0;
}
function add_item($item_id, $qty, $product, $price_id, $price2_id, $price3_id, $price4_id, $extras, $item_price, $item_price2, $item_price3, $item_price4) {
// Item not in cart
if(!isset($this->items[$item_id]))
{
// Add item to cart
$this->items[$item_id]["qty"] = $qty;
$total_cost = $this->items[$item_id]["total_cost"] = $qty * ($item_price + $item_price2 + $item_price3 + $item_price4);
$this->items[$item_id]["product"] = $product;
$this->items[$item_id]["price_id"] = $price_id;
$this->items[$item_id]["price2_id"] = $price2_id;
$this->items[$item_id]["price3_id"] = $price3_id;
$this->items[$item_id]["price4_id"] = $price4_id;
$this->items[$item_id]["extras"] = $extras;
$this->sub_total += $total_cost;
}
else
{
// Update item
$item_count = $this->items[$item_id]["qty"] += $qty;
$this->items[$item_id]["total_cost"] = $item_count * ($item_price + $item_price2 + $item_price3 + $item_price4);
$new_cost = $qty * ($item_price + $item_price2 + $item_price3 + $item_price4);
// Calculate subtotal
$this->sub_total += $new_cost;
}
}
function modify_qty($item_id, $new_qty, $item_price, $item_price2, $item_price3, $item_price4) {
// Modify quantity
$prev_qty = $this->items[$item_id]["qty"];
$qty_diff = $prev_qty - $new_qty;
$this->items[$item_id]["total_cost"] = $new_qty * ($item_price + $item_price2 + $item_price3 + $item_price4);
$this->items[$item_id]["qty"] = $new_qty;
// Calculate new subtotal
$this->sub_total -= $qty_diff * $item_price;
}
function delete_item($item_id) {
// Delete item from cart
$this->sub_total -= $this->items[$item_id]["total_cost"];
$this->items[$item_id] = array();
}
function num_items() {
// Calculate total number of items
$total_items = 0;
foreach($this->items as $item_id => $item_data)
$total_items += $item_data["qty"];
return $total_items;
}
function clear() {
// Clear out the cart
$this->items = array();
$this->sub_total = 0;
}
function showcart() {
//dumps cart contents to screen
$current_url = $_SERVER['PHP_SELF'];
foreach ($this->items as $thekey => $thevalue)
{
//print qty
print "<tr><td align='left' valign='top'>";
print "<div align=right>" . $this->items[$thekey]["qty"] . "</div>";
//print item number
if (!$this->items[$thekey]["product"])
{
print "</td><td align='left' valign='top'>";
print "<div align=center></div>";
}
//print product name
print "</td><td align='left' valign='top'>";
print "<strong> " .($this->items[$thekey]["product"])."</strong><br />";
print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price_id"])." ";
print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price2_id"])." ";
print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price3_id"])." ";
print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price4_id"])." ";
print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["extras"])." ";
//print total cost
print "</td><td align='left' valign='top'>";
print "<div align=right>€" .number_format($this->items[$thekey]["total_cost"], 2, ',', ' '). " </div>";
//print action column
//print "</td><td>";
//print "<form name=form1 method=get action=". $current_url .">";
//print "<input type=submit value=Verwijder name=Submit><input type=hidden name=delete value=1>";
//print "<input name=itemid type=hidden value=" . $thekey . "></form></tr>";
//print "</td><tr>";
}
}
}
<?php class cart { var $items; var $sub_total; function cart() { $sub_total = 0; } function add_item($item_id, $qty, $product, $price_id, $price2_id, $price3_id, $price4_id, $extras, $item_price, $item_price2, $item_price3, $item_price4) { // Item not in cart if(!isset($this->items[$item_id])) { // Add item to cart $this->items[$item_id]["qty"] = $qty; $total_cost = $this->items[$item_id]["total_cost"] = $qty * ($item_price + $item_price2 + $item_price3 + $item_price4); $this->items[$item_id]["product"] = $product; $this->items[$item_id]["price_id"] = $price_id; $this->items[$item_id]["price2_id"] = $price2_id; $this->items[$item_id]["price3_id"] = $price3_id; $this->items[$item_id]["price4_id"] = $price4_id; $this->items[$item_id]["extras"] = $extras; $this->sub_total += $total_cost; } else { // Update item $item_count = $this->items[$item_id]["qty"] += $qty; $this->items[$item_id]["total_cost"] = $item_count * ($item_price + $item_price2 + $item_price3 + $item_price4); $new_cost = $qty * ($item_price + $item_price2 + $item_price3 + $item_price4); // Calculate subtotal $this->sub_total += $new_cost; } } function modify_qty($item_id, $new_qty, $item_price, $item_price2, $item_price3, $item_price4) { // Modify quantity $prev_qty = $this->items[$item_id]["qty"]; $qty_diff = $prev_qty - $new_qty; $this->items[$item_id]["total_cost"] = $new_qty * ($item_price + $item_price2 + $item_price3 + $item_price4); $this->items[$item_id]["qty"] = $new_qty; // Calculate new subtotal $this->sub_total -= $qty_diff * $item_price; } function delete_item($item_id) { // Delete item from cart $this->sub_total -= $this->items[$item_id]["total_cost"]; $this->items[$item_id] = array(); } function num_items() { // Calculate total number of items $total_items = 0; foreach($this->items as $item_id => $item_data) $total_items += $item_data["qty"]; return $total_items; } function clear() { // Clear out the cart $this->sub_total = 0; } function showcart() { //dumps cart contents to screen $current_url = $_SERVER['PHP_SELF']; foreach ($this->items as $thekey => $thevalue) { //print qty print "<tr><td align='left' valign='top'>"; print "<div align=right>" . $this->items[$thekey]["qty"] . "</div>"; //print item number if (!$this->items[$thekey]["product"]) { print "</td><td align='left' valign='top'>"; print "<div align=center></div>"; } //print product name print "</td><td align='left' valign='top'>"; print "<strong> " .($this->items[$thekey]["product"])."</strong><br />"; print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price_id"])." "; print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price2_id"])." "; print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price3_id"])." "; print " " .eregi_replace("[^, ,a-z]", "", $this->items[$thekey]["price4_id"])." "; //print total cost print "</td><td align='left' valign='top'>"; print "<div align=right>€" .number_format($this->items[$thekey]["total_cost"], 2, ',', ' '). " </div>"; //print action column //print "</td><td>"; //print "<form name=form1 method=get action=". $current_url .">"; //print "<input type=submit value=Verwijder name=Submit><input type=hidden name=delete value=1>"; //print "<input name=itemid type=hidden value=" . $thekey . "></form></tr>"; //print "</td><tr>"; } } }
|