hwhite
Junior Member
Joined: Thu Sep 06, 2007 7:09 pm Posts: 1
|
 Cart Class problem using strings for product id
I am using wfcart class for a small project. It works fine except that when you try to pass a string for a product id the cart breaks. Below is the class file
<? class wfCart { var $total = 0; var $itemcount = 0; var $items = array(); var $itemprices = array(); var $itemqtys = array(); var $iteminfo = array();
function cart() {} // constructor function
function get_contents() { // gets cart contents $items = array(); foreach($this->items as $tmp_item) { $item = FALSE;
$item['id'] = $tmp_item; $item['qty'] = $this->itemqtys[$tmp_item]; $item['price'] = $this->itemprices[$tmp_item]; $item['info'] = $this->iteminfo[$tmp_item]; $item['subtotal'] = $item['qty'] * $item['price']; $items[] = $item; } return $items; } // end of get_contents
function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE) { // adds an item to cart if(!$price) { $price = wf_get_price($itemid,$qty); }
if(!$info) { $info = wf_get_info($itemid); }
if($this->itemqtys[$itemid] > 0) { // the item is already in the cart.. // so we'll just increase the quantity $this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid]; $this->_update_total(); print "
test"; } else { $this->items[]=$itemid; $this->itemqtys[$itemid] = $qty; $this->itemprices[$itemid] = $price; $this->iteminfo[$itemid] = $info; print "test";
} $this->_update_total(); } // end of add_item
function edit_item($itemid,$qty) { // changes an items quantity
if($qty < 1) { $this->del_item($itemid); } else { $this->itemqtys[$itemid] = $qty; // uncomment this line if using // the wf_get_price function // $this->itemprices[$itemid] = wf_get_price($itemid,$qty); } $this->_update_total(); } // end of edit_item
function del_item($itemid) { // removes an item from cart $ti = array(); $this->itemqtys[$itemid] = 0; foreach($this->items as $item) { if($item != $itemid) { $ti[] = $item; } } $this->items = $ti; $this->_update_total(); } //end of del_item
function empty_cart() { // empties / resets the cart $this->total = 0; $this->itemcount = 0; $this->items = array(); $this->itemprices = array(); $this->itemqtys = array(); $this->iteminfo = array(); } // end of empty cart
function _update_total() { // internal function to update the total in the cart $this->itemcount = 0; $this->total = 0; if(sizeof($this->items > 0)) { foreach($this->items as $item) { $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]); $this->itemcount++; } } } // end of update_total
} ?>
Here is the demo cart
<?php // wfCart Demo
// You must included wfcart.php BEFORE you start the session. include "wfcart.php"; session_start(); // start the session
$cart =& $_SESSION['wfcart']; // point $cart to session cart. if(!is_object($cart)) $cart = new wfCart(); // if $cart ( $_SESSION['cart'] ) isn't an object, make a new cart
// end of header stuff
?> <html><head><title>wfCart Demo</title></head> <body><h3>wfCart Demo</h3>
<?
// Usually you would get your products from a database but we'll pretend..
$products = array(); $products[1] = array("id"=>1,"name"=>"A Bar of Soap","price"=>2.00); $products[2] = array("id"=>2,"name"=>"Shampoo","price"=>4.80); $products[3] = array("id"=>3,"name"=>"Pizza","price"=>12.95);
// check to see if any items are being added if($_POST['add']) { $product = $products[$_POST['id']]; $cart->add_item($product['id'],$_POST['qty'],$product['price'],$product['name']); } if($_POST['remove']) { $rid = intval($_POST['id']); $cart->del_item($rid);
}
// spit some forms // You can have many different types of forms, such as many quantity boxes // and an "add to cart" button at the bottom which adds all items // but for the purposes of this demo we will handle one item at a time. echo "<table>"; foreach($products as $p) { echo "<tr><td><form method='post' action='demo.php'>"; echo "<input type='hidden' name='id' value='".$p['id']."'/>"; echo "".$p['name'].' $'.number_format($p['price'],2)." "; echo "<input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'>"; echo "</form></td></tr>"; } echo "</table>";
echo "<h2>Items in cart</h2>";
if($cart->itemcount > 0) { foreach($cart->get_contents() as $item) { echo " Item: "; echo "Code/ID :".$item['id']." "; echo "Quantity:".$item['qty']." "; echo "Price :$".number_format($item['price'],2)." "; echo "Info :".$item['info']." "; echo "Subtotal :$".number_format($item['subtotal'],2)." "; echo "<form method=post><input type='hidden' name='id' value='".$item['id']."'/><input type='submit' name='remove' value='Remove'/></form>"; } echo "--------------------- "; echo "total: $".number_format($cart->total,2); } else { echo "No items in cart"; }
?>
As I stated it works fine until you change for example $products[1] = array("id"=>1,"name"=>"A Bar of Soap","price"=>2.00); to $products['abcde'] = array("id"=>1,"name"=>"A Bar of Soap","price"=>2.00);
It will the product, but once you remove it and then try to add it or any other products nothing happens. I can not figure it out.
Thanks in advance for you help
|
hari
Junior Member
Joined: Wed Apr 07, 2010 5:08 am Posts: 1
|
 problem adding attribute using wfcart
<?php class wfCart_shop { var $total = 0; var $itemcount = 0; var $items = array(); var $itemprices = array(); var $itemqtys = array(); var $iteminfo = array(); var $itemattribute = array(); function wfCart_shop() {} // constructor function function get_contents() { //gets cart contents $items = array(); //print_r($items); foreach($this->items as $tmp_item) { $item = FALSE; $item['id'] = $tmp_item; $item['qty'] = $this->itemqtys[$tmp_item]; $item['price'] = $this->itemprices[$tmp_item]; $item['info'] = $this->iteminfo[$tmp_item]; $item['attribute'] = $this->itemattribute[$tmp_item]; //$item['attribute'] = 9; $item['subtotal'] = $item['qty'] * $item['price']; $items[] = $item; } //print_r($item_attribute); echo "<pre>"; print_r($items); echo "</pre>"; //exit; return $items; } //end of get_contents
function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE, $attribute ) { //adds an item to cart
if(!$price) { $price = wf_get_price($itemid,$qty); }
if(!$info) { $info = wf_get_info($itemid); } /* if(!$attribute) { $attribute = wf_get_attribute($itemid); }*/
if($this->itemqtys[$itemid] > 0) { // the item is already in the cart.. // so we'll just increase the quantity $this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid]; $this->_update_total(); } else { $this->items[]=$itemid; //$this->items[]; $this->itemqtys[$itemid] = $qty; $this->itemprices[$itemid] = $price; $this->iteminfo[$itemid] = $info; $this->itemattribute[$itemid] = $attribute; } $this->_update_total(); } // end of add_item
/*function edit_item($itemid,$qty) {*/ function edit_item($itemid,$qty,$attribute) { // changes an items quantity if($qty < 1) { $this->del_item($itemid); $this->item_attribute[$itemid] = $attribute; } else { $this->itemqtys[$itemid] = $qty; $this->item_attribute[$itemid] = $attribute; //echo $this->itemqtys[$itemid]; //uncomment this line if using //the wf_get_attribute function // $this->itemattribute[$itemid] = wf_get_attribute($itemid,$qty,$attribute); } $this->_update_total(); } // end of edit_item
function del_item($itemid) { // removes an item from cart $ti = array(); $this->itemqtys[$itemid] = 0; foreach($this->items as $item) { if($item != $itemid) { $ti[] = $item; } } $this->items = $ti; $this->_update_total(); } //end of del_item
function empty_cart() { // empties / resets the cart $this->total = 0; $this->itemcount = 0; $this->items = array(); $this->itemprices = array(); $this->itemqtys = array(); $this->iteminfo = array(); $this->itemattribute = array(); } // end of empty cart
function _update_total() { // internal function to update the total in the cart $this->itemcount = 0; $this->total = 0; sizeof($this->items); if(sizeof($this->items > 0)) { foreach($this->items as $item) { $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]); $this->itemcount++; } } } // end of update_total] }
?>
please suggest me the solution regarding how to add attribute like color,size using wfcart? I used select box for attribute for eg,red,large via the selct name "sizestyle" but i coluld not get the value via the "$item['attribute'] = $this->itemattribute[$tmp_item];" in get_contents() in the above code.please help
|