Guest eiddam Posted April 10, 2009 Report Share Posted April 10, 2009 I bought the killersite shopping cart DVDs and have got a 'test' shopping cart up on my site successfully. I wanted to add the ability to specify quantity rather than have to repeatedly add a new item (10 times if you want to order 10) etc and also to be able to delete an item or edit quantity from the cart. Is there any way someone could advise me on this please. Many thanks in advance... Quote Link to comment Share on other sites More sharing options...
Guest figuringthingsout Posted July 10, 2009 Report Share Posted July 10, 2009 I too, have been trying to figure this out... any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
Korken Posted July 10, 2009 Report Share Posted July 10, 2009 This may help I just watched the free movies and then worked on the rest by my self. <?php /********************************************************************** * - Made in the Eclipse IDE: www.eclipse.org * - Tested on PHP 5.2 * - Kickstart in OOPHP: www.killerphp.com * * Usage of the class ShoppingCart * * Attention!: product_id is a string and value is an integer. * * -> addItem("product_id"): * This will add an item to the cart. * If the item already exists, the quantity of that item will increased by 1. * * -> deleteItem("product_id"): * This will remove an item from the cart. * Remove as in it will be gone, if you want to change the quantity see 'SetQuantity'. * * -> getItems(): * This will return an array with all the items in the cart. * * -> setQuantity("product_id", value): * This will change the quantity of the item to value. * If value is 0 the item will be automatically removed. * * -> getQuantity("product_id"): * This will return the quantity of the item. * * * Design of the cart array: * _______________________ * | Product ID | Quantity | * | (string) | (int) | * |------------|----------| * | exampleid1 | 5 | * |- - - - - - | - - - - -| * | exampleid2 | 3 | * |- - - - - - | - - - - -| * | exampleid5 | 13 | * ----------------------- **********************************************************************/ class ShoppingCart { private $cart = array(); public function addItem($product_id) { if (array_key_exists($product_id, $this->cart)) $this->cart[$product_id] += 1; else $this->cart[$product_id] = 1; } public function deleteItem($product_id) { if (array_key_exists($product_id, $this->cart)) unset($this->cart[$product_id]); } public function getItems() { return array_keys($this->cart); } public function setQuantity($product_id, $value) { if (array_key_exists($product_id, $this->cart)) { if ((int)$value > 0) $this->cart[$product_id] = (int)$value; else unset($this->cart[$product_id]); } } public function getQuantity($product_id) { if (array_key_exists($product_id, $this->cart)) return $this->cart[$product_id]; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.