shiprate["French_Postal"][1][0]["maxweight"] = 100; // $this->shiprate["French_Postal"][1][0]["cost"] = 1.04; // $this->shiprate["French_Postal"][2][0]["maxweight"] = 100; // $this->shiprate["French_Postal"][2][0]["cost"] = 1.14; /* 200g */ // $this->shiprate["French_Postal"][1][1]["maxweight"] = 200; // $this->shiprate["French_Postal"][1][1]["cost"] = 1.98; // $this->shiprate["French_Postal"][2][1]["maxweight"] = 200; // $this->shiprate["French_Postal"][2][1]["cost"] = 2.06; function Cart() { $this->init(); } /** * initialize (and reset) a shopping cart */ function init() { $this->items = array(); $this->subtotals = array(); $this->total = 0; $this->weight = 0; $this->tax = 0; $this->delivery = 0; $this->final_price = 0; } /** * Checks item quantity in stock to make sure there are enough. Returns * the total number of items in stock. * * @param mixed $product_id id of the item to check * * @return int the number of items in stock, 0 if none */ function inventory($product_id) { $qid = dbQuery("SELECT qty_in_stock FROM product_tbl WHERE product_id = '$product_id'"); $in_stock = mysql_fetch_row($qid); return $in_stock[0]; } /** * Add $qty items to the shopping cart, adding to the number already selected. * If there are not enough items in stock, the total number of items in stock are set. * Returns the quantity it was set to. * * @param mixed $product_id the id of the item to add * @param int $qty the quantity of items to add * * @return int the number of items that were added. May not be the * same number as were requested if not enough are available */ function addItem(&$product_id, $qty) { $in_stock = $this->inventory($product_id); $new_qty = $this->items[$product_id] + $qty; if ($new_qty <= $in_stock && isset($product_id)) { /* If there are enough items in stock and the product id is specified, we add them to the cart */ $this->items[$product_id] = $new_qty; } else { /* Otherwise we set the qty to the actual number of items in stock */ $new_qty = $in_stock; $this->items[$product_id] = $new_qty; } return $new_qty; } /** * Set the quantity of a product in the cart to a specified value. * Returns the quantity it was set to. * * @param mixed $product_id the id of the item to set * @param int $new_qty the quantity of items to set * * @return int the number of items that the cart was set to. May not be the * same number as were requested if not enough are available */ function setItemQty(&$product_id, $new_qty) { $in_stock = $this->inventory($product_id); if ($new_qty <= $in_stock && isset($product_id)) { $this->items[$product_id] = (int) $new_qty; } else { $new_qty = $in_stock; $this->items[$product_id] = $new_qty; } return $new_qty; } /** * this function will remove a given product from the cart * * @param mixed $product_id item id to remove */ function removeItem(&$product_id) { if (isset($product_id)) { unset($this->items[$product_id]); } } /** * this function will clean up the cart, removing items with invalid product * id's (non-numeric ones) and products with quantities less than 1. */ function cleanup() { foreach ($this->items as $product_id => $qty) { if ($qty < 1) { unset($this->items[$product_id]); } } } /** * Returns the total number of individual items in the shopping cart * (note, this takes into account the quantities of the items as well). * If a product_id is specified, returns the count of just that item. * * @param mixed $product_id optional id of item to count * * @return int the number of items in the cart */ function itemCount($product_id='') { $count = 0; if (empty($product_id)) { foreach ($this->items as $item => $qty) { $count += $qty; } } else { foreach ($this->items as $item => $qty) { if ($product_id == $item) { $count += $qty; } } } return $count; } /** * Return a comma delimited list of all the products in the cart, this will * be used for queries, eg. SELECT id, price FROM products WHERE id IN ... * * @return string a comma delimited list of the item ids in the cart */ function getProductidList() { $comma = ''; foreach ($this->items as $product_id => $qty) { $product_id_list .= $comma . "'" . $product_id . "'"; $comma = ','; } return $product_id_list; } /** * return an array of all the items in the shopping cart * * @return array a 2-dimentional array, containing the result set of the query */ function getCartItems() { $in_clause = $this->getProductidList(); if (empty($in_clause)) { return array(); } $qid = dbQuery(" SELECT * FROM product_tbl WHERE product_id IN ($in_clause) "); while ($row = mysql_fetch_assoc($qid)) { $ret[] = $row; } return $ret; } /** * recalculate the total for the shopping cart, we will also do some cleanup * and remove invalid items from the cart. we have to query the database to * get the prices, so instead of making one query for each product in the * basket, we will gather up all the ID's we are interested in and run one * query to get all the products we care about (using $in_clause). * * @return bool true on success, false on failure */ function recalculateTotal() { $this->total = 0; $this->weight = 0; $this->tax = 0; $this->delivery = 0; $this->final_price = 0; $in_clause = $this->getProductidList(); if (empty($in_clause)) { return; } $qid = dbQuery("SELECT product_id, retail_price, weight FROM product_tbl WHERE product_id IN ($in_clause)"); while ($product = mysql_fetch_object($qid)) { $this->subtotals[$product->product_id] = $this->items[$product->product_id] * $product->retail_price; $this->total += $this->subtotals[$product->product_id]; $this->weight += $this->items[$product->product_id] * $product->weight; } $this->delivery = $this->getDeliveryCost($this->weight); $this->tax = $this->getTaxCost($this->total); $this->final_price = $this->total + $this->tax + $this->delivery; } /** * Returns the cost of delivery based on the supplied variables. * * @param float $total_weight weight of all items combined * * @param string $delivery_type optional delivery type to return the cost * for this type of delivery. could be used * to show a price for each method available. * * @param string $zone optional zone for getting a specific price * * @return float the cost of delivery */ function getDeliveryCost($total_weight, $delivery_type='', $zone='') { global $_SESSION; if ($total_weight == 0) { return 0; } setDefault($_SESSION['app']['orderinfo']['delivery_type'], $this->default_delivery_method); if (empty($delivery_type)) { $delivery_type = $_SESSION['app']['orderinfo']['delivery_type']; } setDefault($_SESSION['app']['orderinfo']['delivery_zone'], $this->default_delivery_zone); if (empty($zone)) { $zone = $_SESSION['app']['orderinfo']['delivery_zone']; } if ($zone == 0) { $zone = $this->default_delivery_zone; } for ($i=sizeof($this->shiprate[$delivery_type][$zone]); $i>=0; $i--) { if ($total_weight <= $this->shiprate[$delivery_type][$zone][$i]['maxweight']) { // && $this->total <= $this->shiprate[$delivery_type][$zone][$i]['maxprice'] $delivery_cost = $this->shiprate[$delivery_type][$zone][$i]['cost']; } } return $delivery_cost; } /** * Returns the cost of tax based on the default US billing state. CA is set here. * * @param float $total the total cost of this order before tax or shipping * * @return float the cost of the tax */ function getTaxCost($total) { global $_SESSION; if (!$this->we_charge_tax) { return 0; } setDefault($_SESSION['app']['orderinfo']['bill_state'], 'CA'); foreach ($this->taxrate as $state => $rate) { if (eregi($state, $_SESSION['app']['orderinfo']['bill_state'])) { $tax = ($rate / 100) * $total; break; } else { $tax = 0; } } return $tax; } } ?>