ruạṛ
<?PHP /** * shopping cart object * may contain product objects */ class cart { /** * @var string $reference order reference * @var float $cart_total values of goods in the cart * @var int $num_items quantity of goods in the cart * @var int $num_products number of distinct products in the cart * @var array $products container for product objects * @var array $customer contains customer details from checkout as array items or as an object * @var float $transaction_fee extra cost on order for transaction charges eg credit card, paypal, paymex * @var int $ship_id db id of shipping category * @var string $shipping_location text of shipping category * @var float $shipping_cost * @var string $discount_code * @var string $discount_type * @var string $discount_var * @var float $discount_amount * @var string $description * @var bool $paid paid status */ var $reference , $cart_total , $num_items , $num_products , $products , $transaction_fee , $ship_id , $shipping_location , $shipping_cost , $discount_code , $discount_type , $discount_var , $discount_amount , $description , $paid ; var $customer = array(); /** * object construction function */ function __construct() { $this->empty_cart(); return; } /** * generate the in-cart unique reference for a product * this won't necessarily give the highest $i, if a product in the series has been deleted it will just give the first empty space * which is fine unless there is a need for the items to be ordered by when added * @param $prod_id product id * @return string */ function generate_product_reference($prod_id) { $i=0; //start at 0 because we're effectively shrinking a nested array while(isset($this->products["$prod_id:$i"])) { $i++; } return "$prod_id:$i"; } /** * split product id out of cart item reference * @param string $ref starting with $prod_id * @return int */ function split_reference($reference) { $parts = explode(':',$reference); return $parts[0]; } /** * modify cart contents * @param string $reference unique reference to item in cart or product id of item not yet in cart * @param int $qty quantity * @param array $selected * @param int $cat_id * @return bool */ function product($reference,$qty=0,$selected=array(),$cat_id=0) { /** * get product ID from reference (may be entire string) * @var int $prod_id product id */ $prod_id = $this->split_reference($reference); if($this->products["$reference"]) { /** * alter quantity of product in cart * @var object $p short reference to manipulate cart item directly * @todo options comparison * @var int $instance position in the individual product quantiy and options arrays */ $p = &$this->products["$reference"]; //alias to short reference $diff = $qty - $p->quantity; $p->quantity += $diff; if($p->quantity<=0) { /* * remove product from cart */ $diff -= $p->quantity; //this makes sure we don't remove more items fron the cart than had been added by adding back any difference below 0 unset($this->products["$reference"]); //can't just use $p here $this->num_products--; } elseif(!empty($selected)) { /* * update selected options */ $p->selected = $options; } $this->cart_total += $p->price * $diff; $this->num_items += $diff; } elseif($qty>0) //can't add a nil quantity of a new product { /** * add a new product to the cart * prod_id is the naked $reference * @uses object $customer * @var object $p product object */ global $customer; $p = new product($reference); if(!$p->active){ return false; } //can't add products which are not active in the store (or don't exist) //else $p->quantity = $qty; $p->selected = $selected; $p->set_category($cat_id); if($customer->wholesale) { $p->price = $p->wholesale; } if($customer->discount) { $p->price -= $p->set_discount('%',$customer->discount); } /* * get unique in-cart reference and store item */ $reference = $this->generate_product_reference($reference); $this->products["$reference"] = $p; $this->cart_total += $p->price * $qty; $this->num_items += $qty; $this->num_products++; } return true; } /** * see if a product is in the cart and return reference * @param string $prod_id * @return array */ function contains_product($prod_id) { $instances = array(); $references = array_keys($this->products); foreach($this->products as $ref => $p) { if($this->split_reference($ref)==$prod_id) { $instances[$ref] = $p; } } return $instances; } /** * regenerate cart with latest price information * @todo check for valid option sets */ function update_prices(){ /** * copy $this->products into local scope, empty cart, re-add each product with qty and options * might be more efficient by some metric to iterate through $this->products and just pull out prod_id and qty. * @var array $copy_products */ $copy_products = $this->products; $this->cart_total = $this->num_items = $this->num_products = 0; $this->products = array(); foreach($copy_products as $reference => $p) { $prod_id = $this->split_reference($reference); $this->product($p->prod_id,$p->quantity,$p->options,$p->cat_id); } return; } /** * merge another cart with this one * @param object $cart incoming cart * @param bool $duplicates allow exact duplicates (not implemented) * @todo duplicate checking and combining */ function merge($cart, $duplicates=true){ foreach($cart->products as $reference => $p) { $this->product($p->prod_id,$p->quantity,$p->options,$p->cat_id); } return; } /** * get shipping details from database based on ship_id */ function set_shipping() { if(!$this->ship_id) { $this->shipping_location = ''; $this->shipping_cost = 0; } else { $shipping = mysql_query("select * from shipping where ship_id ='".$this->ship_id ."'"); if(!$shipping || mysql_num_rows($shipping)<1) { $this->shipping_location = ''.myslq_error(); $this->shipping_cost = 0+$this->ship_id; } else { $s = mysql_fetch_assoc($shipping); $this->shipping_location = $s['location']; $this->shipping_cost = $s['shipping_cost']; } } return; } /** * calculate discount based on supplied % * @param string $discount_type (ie '%', '$') * @param float $discount (% or $ eg 12.5, 15, 25.50) * @param string $discount_code * @return float */ function set_discount($discount_type='%',$discount=0,$discount_code='') { $this->discount_code = $discount_code; $this->discount_type = $discount_type; $this->discount_var = $discount; switch($discount_type) //whitelist { case '$': $this->discount_amount = min($discount,$this->cart_total); //don't allow -ve values, excludes shipping break; case '%': $this->discount_amount = round(($this->cart_total/100)*$discount,2); //($this->cart_total/100)*$percent == $this->cart_total*($percent/100) but reduces the chance of floating point and rounding error break; default: //invalid value $this->discount_amount = 0; $this->discount_type = ''; $this->discount_var = 0; } return $this->discount_amount; } /** * format discount for display * @return string */ function discount_format() { switch($this->discount_type) { case '$': return format_price($this->discount_var,2); case '%': return $this->discount_var.'%'; default: return ''; } } /** * return order total - always calculate this so we don;t have to worry about updating it every time * we change one of the components */ function order_total() { return $this->cart_total - $this->discount_amount + $this->shipping_cost + $this->transaction_fee; } function empty_cart() { $this->products = array(); $this->customer = array(); $this->reference = $this->discount_code = $this->description = ''; // $this->shipping_location = ''; $this->cart_total // = $this->ship_id = $this->shipping_cost = $this->discount_amount = $this->transaction_fee = $this->num_items = $this->num_products = $this->paid = 0; return; } /** * recreate * @param int $order_id */ function recreate_from_order($order_id) { $order = mysql_query("select * from orders where order_id = '$order_id'"); if(!$order || mysql_num_rows($order)<1) { //die(mysql_error()); //debugging return; } //else $products = mysql_query("select * from order_products where order_id = '$order_id'"); $temp = mysql_query("select * from order_temp where order_id = '$order_id'"); $o = mysql_fetch_assoc($order); $this->reference = $o['order_ref']; $this->description = $o['order_description']; $this->ship_id = select_one('shipping','ship_id','location',$o['shipping_location']); $this->shipping_location = $o['shipping_location']; $this->shipping_cost = $o['shipping_cost']; $this->discount_code = $o['discount_code']; $this->discount_amount = $o['discount_amount']; $this->paid = $o['paid']; $o['customer_name'] = html_entity_decode($o['customer_name'],ENT_QUOTES); $names = explode(' ',$o['customer_name'],2); $this->customer['name'] = $o['customer_name']; $this->customer['first_name'] = $names[0]; $this->customer['last_name'] = $names[1]; $this->customer['phone'] = html_entity_decode($o['customer_phone'],ENT_QUOTES); $this->customer['email'] = $o['customer_email']; $this->customer['delivery_address'] = html_entity_decode($o['delivery_address'],ENT_QUOTES); $this->customer['delivery_instructions'] = html_entity_decode($o['delivery_instructions'],ENT_QUOTES); $this->customer['payment_method'] = $o['payment_method']; $this->transaction_fee = $o['transaction_fee']; if($temp && mysql_num_rows($temp)>0) { $t = mysql_fetch_assoc($temp); $this->customer['json'] = html_entity_decode($t['json'],ENT_QUOTES); $this->customer['email_text'] = html_entity_decode($t['email_text'],ENT_QUOTES); } if($products && mysql_num_rows($products)>0) { $i = 0; while($pr = mysql_fetch_assoc($products)) { $p = new product; $p->prod_id = $prod_id = $this->split_reference($pr['product_id']); $p->name = $pr['product_name']; $p->price = $price = $pr['price']; $p->quantity = $qty = $pr['quantity']; $p->qDesc = $pr['quantity_description']; $p->selected = json_decode($pr['options']); $p->set_category($pr['category_id'],true); /* * get unique in-cart reference and store item */ $reference = $this->generate_product_reference($prod_id); $this->products["$reference"] = $p; $this->cart_total += $price * $qty; $this->num_items += $qty; $this->num_products++; } } return; } } ?>
cải xoăn