Jump to content

Adding a quantity and a delete function to your shopping cart


Guest eiddam

Recommended Posts

Guest eiddam

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

Link to comment
Share on other sites

  • 3 months later...

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];
   }
}
?>

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...