Jump to content

Shopping Cart Class


MasterACE14

Recommended Posts

Hello,

 

I've been using this shopping cart class script and it's all working fine but I need to add 2 more things;

1) get prices for items from a table in a database, based on their $order_code in the getItemPrice() method.

2) add a method to 'purchase' the items in the shopping cart, by inserting the items into a table in a database.

 

here's the script:

<?php

class Shopping_Cart {
var $cart_name;       // The name of the cart/session variable
var $items = array(); // The array for storing items in the cart

/**
 * __construct() - Constructor. This assigns the name of the cart
 *                 to an instance variable and loads the cart from
 *                 session.
 *
 * @param string $name The name of the cart.
 */
function __construct($name) {
	$this->cart_name = $name;
	$this->items = $_SESSION[$this->cart_name];
}

/**
 * setItemQuantity() - Set the quantity of an item.
 *
 * @param string $order_code The order code of the item.
 * @param int $quantity The quantity.
 */
function setItemQuantity($order_code, $quantity) {
	$this->items[$order_code] = $quantity;
}

/**
 * getItemPrice() - Get the price of an item.
 *
 * @param string $order_code The order code of the item.
 * @return int The price.
 */
function getItemPrice($order_code) {
	// This is where the code taht retrieves prices
	// goes. We'll just say everything costs $9.99 for this tutorial.
	return 3;
}

/**
 * getItemName() - Get the name of an item.
 *
 * @param string $order_code The order code of the item.
 */
function getItemName($order_code) {
	// This is where the code that retrieves product names
	// goes. We'll just return something generic for this tutorial.
	return '' . $order_code . '';
}

/**
 * getItems() - Get all items.
 *
 * @return array The items.
 */
function getItems() {
	return $this->items;
}

/**
 * hasItems() - Checks to see if there are items in the cart.
 *
 * @return bool True if there are items.
 */
function hasItems() {
	return (bool) $this->items;
}

/**
 * getItemQuantity() - Get the quantity of an item in the cart.
 *
 * @param string $order_code The order code.
 * @return int The quantity.
 */
function getItemQuantity($order_code) {
	return (int) $this->items[$order_code];
}

/**
 * clean() - Cleanup the cart contents. If any items have a
 *           quantity less than one, remove them.
 */
function clean() {
	foreach ( $this->items as $order_code=>$quantity ) {
		if ( $quantity < 1 )
			unset($this->items[$order_code]);
	}
}

/**
 * save() - Saves the cart to a session variable.
 */
function save() {
	$this->clean();
	$_SESSION[$this->cart_name] = $this->items;
}
}

?>

 

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/212655-shopping-cart-class/
Share on other sites

I need to add 2 more things

 

No, you don't. Open/Closed Principle

 

1) get prices for items from a table in a database, based on their $order_code in the getItemPrice() method.

 

Due to the Shopping_Cart design (or lack thereof) this becomes more difficult to implement as an item alone should know how much it costs. You can solve this by using the Proxy Pattern:

 

class Shopping_CartProxy extends Shopping_Cart {
    function getItemPrice($order_code) {
        $db->query(..);
        return ..;
    }
}

 

Use the proxy as if it is the Shopping_Cart (if you use a Factory Method to create the Shopping_Cart you'll only have to modify the Factory Method)

 

public function someFunction(Shopping_Cart $sc) {
   ..
}

 

$proxy = new Shopping_CartProxy();
..
$o->someFunction($proxy);

 

2) add a method to 'purchase' the items in the shopping cart, by inserting the items into a table in a database.

 

class ShoppingCartRepository {
    function purchase(Shopping_Cart $sc) {
      $db->query(..);
    }
}

Link to comment
https://forums.phpfreaks.com/topic/212655-shopping-cart-class/#findComment-1107817
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.