an OOP Shopping cart: calculate the total cost in the cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

an OOP Shopping cart: calculate the total cost in the cart

Post by lauthiamkok »

Code: Select all

class _cart
{
	
	 private $_contents = array();
	 private $_created;
	 
	 /**
	  * The class constructor
	  *
	  */
	 public function __construct() {
         $this->_created = time();
	 }
	 
	 /**
	  * Add a product to the cart
	  * @access public
	  * @param $productID integer
	  *
	  */
	 public function add_item($productID) {

	 	if (isset($this->_contents[$productID])) {
	 	    $this->_contents[$productID]++;	
	 	} else {
	 		$this->_contents[$productID] = 1;
	 	}
		
		//print_r($this->_contents);
	 }
	 
	 /**
	  * Remove product from cart
	  * @access public
	  * @param $productId integer
	  *
	  */
	 public function delete_item($productID) {
	 	
 		if (isset($this->_contents[$productID])) {
 			unset($this->_contents[$productID]);
 		}
	 		
	 }
	 
	 /**
	  * Change the quantity of a particular item held in the shopping cart
	  *
	  * @access public
	  * @param integer $productID
	  * @param integer $quantity
	  */
	 public function update_quantity($productID, $quantity) {
	 	$this->_contents[$productID] = $quantity;
	 }
	 
	 /**
	  * Get all items currently in cart
	  *
	  * @access public
	  * @return unknown
	  */
	 public function get_items() {
	 	return $this->_contents;
	 }
	 
	 /**
	  * How many items are in the user's cart?
	  * 
	  * @access public
	  * @return INTEGER
	  *
	  */
	 public function count_items() {
	 	return array_sum($this->_contents);
	 }

	 /**
	  * Calculate the cost of all items in the cart
	  * @access public
	  * @return float
	  *
	  */
	 public function calculate_cost() 
	 {
	 	
	 	$cost = 0.00;
	 	
	 	foreach($this->_contents AS $id => $quantity) {
	 		$product = new _product($id);
	 		$cost = $cost + ($product -> price * $quantity);
	 	}
	 	
	 	return number_format($cost, 2);
	 	
	 }
	 
}
I have tested the methods in the cart class above which I got it from the link here,

http://www.easyphpwebsites.com/blog/rea ... g-with-PHP

But the method of calculate_cost() requires another class - _product class I think!??

Any one know how to create this product class?

I tried to imagine how this _product class is like, so this is my version,

Code: Select all

class _store
{	
	function get_store()
	{
		return new SimpleXMLElement(file_get_contents(STORE_XML));
	}
}

class _product
{
	public $_product_id = null;
	
	public function __construct($id) 
	{
		$this->_product_id = $id;	
	}

	public function get_price() 
	{
		$object_store = new _store();
		$store = $object_store -> get_store();
	   
		foreach ($store as $product)
		{
			if ($product -> id == $this->_product_id)
			{
				return $product -> price;
			}
				
		}	
	}
}
however, to use this _product class the version of mine, will require to change the calculate_cost() into this below which I think it is not looking good at all as it needs a few more lines... and I don't think my _prodcut class is looking great!

Code: Select all

public function calculate_cost() 
	 {
	 	
	 	$cost = 0.00;
	 	
	 	foreach($this->_contents AS $id => $quantity) {
	 		$product = new _product($id);
			$cost_string = $product -> get_price();
			$cost_float = "$cost_string" + 0;
			$cost = $cost + ($cost_float * $quantity);
	 	}
	 	
	 	return number_format($cost, 2);
	 	
	 }
Many thanks,
Lau
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: an OOP Shopping cart: calculate the total cost in the ca

Post by Christopher »

You should be able to just do:

Code: Select all

                foreach($this->_contents AS $id =>  $quantity) {
                        $product = new _product($id);
                        $cost = $cost + ($product -> get_price() * $quantity);
                }
PHP will convert types for you.
(#10850)
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: an OOP Shopping cart: calculate the total cost in the ca

Post by lauthiamkok »

Christopher wrote:You should be able to just do:

Code: Select all

                foreach($this->_contents AS $id =>  $quantity) {
                        $product = new _product($id);
                        $cost = $cost + ($product -> get_price() * $quantity);
                }
PHP will convert types for you.
thanks for the reply. i tried this before but it wont work unless you only have round numbers like £2, £4, etc. but not with like £2.99 or £3.99, etc
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: an OOP Shopping cart: calculate the total cost in the ca

Post by Christopher »

Can you be more specific about how it "won't work"? How is the calculation incorrect?
(#10850)
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: an OOP Shopping cart: calculate the total cost in the ca

Post by lauthiamkok »

Christopher wrote:Can you be more specific about how it "won't work"? How is the calculation incorrect?
for instance, £2.99 plus £3.99 should be £6.98, but it only gives you £5

and if only £2.99, it will turn out £2 only...

I can't figure it out the reason of it... :banghead:
Post Reply