modified Jun 3, 2024 Added Cart Constant definitions VHB Ship Cost //-->modified Jul 19, 2022 Added Register Address, redirect when Member has no registered Primary Shipping Address //-->@Reg Shipping Address URL $reg_address_url=$site_url.'/reg_ship_address.php'; //-->@helper functions function CartSetLocalTZ($l_tz='Central') { if (date_default_timezone_get()) { $dtz=date_default_timezone_get(); } else if (ini_get('date.timezone')) { $dtz=ini_get('date.timezone'); } // Set the default timezone to use. Available as of PHP 5.1 if($l_tz == 'Central') { date_default_timezone_set('America/Chicago'); } //--> revert timezone back to old setting // if($dtz) date_default_timezone_set($dtz); } //---- //-->@header js include $hdr_js_code=' '; //-->@Set Local Time //CartSetLocalTZ(); $v_cart_js=<< $(document).ready(function() { $(window).keydown(function(event){ if(event.keyCode == 13) { event.preventDefault(); return false; } }); }); EOD; if(empty($_COOKIE[PHPSESSID])) { //--> start new session session_start(); $my_order_id=$_COOKIE[PHPSESSID]; } else { $my_order_id=$_COOKIE[PHPSESSID]; } // end if, check session var //-->@get member ID //-->@Check IS Member Logged In $x_mbr_id=intval($_SESSION['mbr_id']); if($x_mbr_id >0) { $isLoggedIn=true; } else { $isLoggedIn=false; } // end if, set logged in or not //---- //-->@mod Jul 15, 2022 //-->@Add Primary Shipping Address Register Required, before View Cart if($isLoggedIn == true) { $q_addr_primary="SELECT * from addr_book ab LEFT JOIN members m ON(ab.mbr_id=m.mbr_id) WHERE (m.mbr_id='$x_mbr_id') AND (ab.addr_id=m.mbr_addr_id)"; //---- $r_addr_primary=mysql_query($q_addr_primary); if( ($r_addr_primary) && (mysql_num_rows($r_addr_primary) <=0) ) { //-->@No registered address, forward to reg address form //--> invalid city state info header ("Location: $reg_address_url"); exit(); // exit script } // end if, does member have Registered Primary Shipping Address, if not then forward to reg address form } // end if, is member logged in //---- //-->@cart notification //cart_notification //-->@check for cart notify messages and parse and display them if they are set if( (isset($_SESSION['cart_notify_msg'])) && ($_SESSION['cart_notify_msg'] !='') ) { $cart_notify_msg=$_SESSION['cart_notify_msg']; //echo "cart_notify_msg: $cart_notify_msg
"; //-->@clear and unset this notification message session var $_SESSION['cart_notify_msg']=''; unset($_SESSION['cart_notify_msg']); //-->@parse the cart notify message $cart_notify_rows=split("\n",$cart_notify_msg); $cart_notify_id_arr=array(); $cart_notify_name_arr=array(); $cart_notify_price_arr=array(); $cart_notify_type_arr=array(); $cart_notify_val_arr=array(); //-->@get ItemID, msg_type, msg_value foreach($cart_notify_rows as $cart_msg_line) { $msg_parts=split('~',$cart_msg_line); $msg_item_id=intval($msg_parts[0]); if($msg_item_id >0) { //--> save for query... $cart_notify_id_arr[$msg_item_id]=$msg_item_id; $msg_type_txt=$msg_parts[1]; if( stristr($msg_type_txt,':') ) { //-->@msg_type has values $msg_type_parts=split(':',$msg_type_txt); $msg_type=$msg_type_parts[0]; $cart_notify_type_arr[$msg_item_id]=$msg_type; $msg_type_val=$msg_type_parts[1]; $cart_notify_val_arr[$msg_item_id]=$msg_type_val; } else { $msg_type=$msg_type_txt; // no values for this msg type $cart_notify_type_arr[$msg_item_id]=$msg_type; $cart_notify_val_arr[$msg_item_id]=''; } // end if, msg_type } // end if, ItemID > zero } // end foreach, message row //-->@make id csv list $msg_id_csv_list=implode(',',array_keys($cart_notify_id_arr)); $q_msg_info="SELECT ItemID,ItemName,ItemPrice FROM dd_catalog WHERE ItemID IN ($msg_id_csv_list) ORDER BY ItemID Desc"; $r_msg_info=mysql_query($q_msg_info); if( ($r_msg_info) && (mysql_num_rows($r_msg_info) >0) ) { while($a_msg_info=mysql_fetch_assoc($r_msg_info)) { $msg_item_id=intval($a_msg_info['ItemID']); $msg_item_name=$a_msg_info['ItemName']; $msg_item_price=$a_msg_info['ItemPrice']; $cart_notify_name_arr[$msg_item_id]=$msg_item_name; $cart_notify_price_arr[$msg_item_id]=$msg_item_price; } // end while, fetch //-->@make table rows foreach($cart_notify_id_arr as $key => $val) { $msg_item_id=intval($key); $msg_item_name=$cart_notify_name_arr[$msg_item_id]; $msg_item_price=$cart_notify_price_arr[$msg_item_id]; $msg_type=$cart_notify_type_arr[$msg_item_id]; $msg_type_val=$cart_notify_val_arr[$msg_item_id]; switch($msg_type) { case 'OOS': $cart_notify_tbl_row.=' • Out Of Stock:
'.$msg_item_name.' '; break; case 'MAX': $cart_notify_tbl_row.=' • Max Quantity Available ('.$msg_type_val.'):
'.$msg_item_name.' '; break; default: } // end switch, msg_type //-->@make cart notify table if($cart_notify_tbl_row !='') { //-->@open notify table $cart_notification=' '.$cart_notify_tbl_row.'
 Note: Your shopping cart has been modified
'; } // end if, table rows } // end foreach, } // end if, query item names and prices } // end if, cart notify message //get the cart content $q1 = "select oc.*, clg.* from dd_orders_content as oc JOIN dd_catalog as clg where (oc.OrderID = '$my_order_id') AND (oc.ItemID = clg.ItemID) AND (product_in_stock = 1) AND (clg.priceStruct=0 OR clg.priceStruct=2) order by clg.shipping_group DESC, clg.ItemName"; $r1 = mysql_query($q1) or die(mysql_error()); if(mysql_num_rows($r1) == '0') { //-->@Cart is Empty include_once('templates/HeaderTemplate.php'); include_once('templates/EmptyCartTemplate.php'); } else { //-->@Physical Product, delivery details required //$delivery_details $ph_prod_count=0; // default zero $q_physical_prod="SELECT DISTINCT SUM(oc.ItemQty) as p_count from dd_orders_content as oc LEFT JOIN dd_catalog as clg ON(oc.ItemID = clg.ItemID) where (oc.OrderID = '$my_order_id') AND (clg.priceStruct=0 OR clg.priceStruct=2) AND (clg.prodType=1) GROUP BY oc.OrderID"; $r_physical_prod=mysql_query($q_physical_prod); if( ($r_physical_prod) && (mysql_num_rows($r_physical_prod) >0) ) { $a_physical_prod=mysql_fetch_assoc($r_physical_prod); //-->@get how many physical products in the order need shipping $ph_prod_count=intval($a_physical_prod['p_count']); } // end if, check any physical products in cart //-->@show delivery details when needed if( ($ph_prod_count >0) && ($x_mbr_id > 0) ) { //-->@check Shipping Address changed for this Order $my_mbr_id_mul=(( (1-$x_mbr_id & 1))*68)+(($x_mbr_id & 1)*27)+(($x_mbr_id)*131); $my_order_ch_hash=md5($my_order_id.$my_mbr_id_mul); //---- $q_addr_ch="SELECT order_ship_addr_id from members m WHERE (m.mbr_id='$x_mbr_id') AND (m.order_change_hash='$my_order_ch_hash') AND (m.order_ship_addr_id != 0)"; $r_addr_ch=mysql_query($q_addr_ch); if( ($r_addr_ch) && (mysql_num_rows($r_addr_ch) >0) ) { $a_addr_ch=mysql_fetch_assoc($r_addr_ch); $ch_addr_id=intval($a_addr_ch['order_ship_addr_id']); //-->@use address change $q_addr_single="SELECT * from addr_book ab LEFT JOIN members m ON(ab.mbr_id=m.mbr_id) WHERE (m.mbr_id='$x_mbr_id') AND (ab.addr_id='$ch_addr_id')"; } else { //-->@use primary address $q_addr_single="SELECT * from addr_book ab LEFT JOIN members m ON(ab.mbr_id=m.mbr_id) WHERE (m.mbr_id='$x_mbr_id') AND (ab.addr_id=m.mbr_addr_id)"; } // end if, address change //---- $r_addr_single=mysql_query($q_addr_single); if( ($r_addr_single) && (mysql_num_rows($r_addr_single) >0) ) { $a_addr_single=mysql_fetch_assoc($r_addr_single); //-->@address components $addr_id=$a_addr_single['addr_id']; $addr_first_name=$a_addr_single['first_name']; $addr_last_name=$a_addr_single['last_name']; $addr_country=$a_addr_single['country']; $addr_street1=$a_addr_single['street1']; $addr_street2=$a_addr_single['street2']; $addr_city=$a_addr_single['city']; $addr_state=$a_addr_single['state']; $addr_zip=$a_addr_single['zip']; $addr_p_number=$a_addr_single['p_number']; $addr_p_ext=$a_addr_single['p_ext']; $addr_p_ext_text=($addr_p_ext) ? 'x'.$addr_p_ext.'':''; $addr_p_type_ltr=$a_addr_single['p_type']; switch($addr_p_type_ltr) { case 'M': $addr_p_type_text='Mobile'; break; case 'H': $addr_p_type_text='Home'; break; case 'W': $addr_p_type_text='Work'; break; default: $addr_p_type_text='Mobile'; } // end switch, addr_p_type_ltr //$addr_report=print_r($a_addr_single,true); //-->@change address link $addr_change_link='Change'; //---- $addr_div='

Shipping Address

'.$addr_change_link.'
Country: '.$addr_country.'  
First Name: '.$addr_first_name.' Last Name: '.$addr_last_name.'
Street: '.$addr_street1.'
Street 2: '.$addr_street2.'
City/State: '.$addr_city.', '.$addr_state.'   Zip Code: '.$addr_zip.'
Phone: '.$addr_p_number.' '.$addr_p_ext_text.' Phone Type: ('.$addr_p_type_text.')
'; } // end if, query, q_addr_single //---- $delivery_details='
'.$addr_div.'
'; //---- } else if( ($ph_prod_count >0) && ($x_mbr_id == 0) ) { //-->@Physical product and not logged in $isLoggedIn=false; $delivery_details='
ATTENTION: You are not currently logged in.
Your Order contains ('.$ph_prod_count.') item(s) that require shipping but we have no Address or Account ID.
Please login or Click Here to Register and complete your Order.
'; } else if( ($x_mbr_id == 0) ) { //-->@not logged in $isLoggedIn=false; $delivery_details='
ATTENTION: You are not currently logged in.
Please login or Click Here to Register and complete your Order.
'; } // end if, delivery details //--> coupon info //--> check for coupon if(isset($_SESSION[coupon_code])) { $x_coupon_code=$_SESSION[coupon_code]; } // end if, check for coupon //--> pull coupon info, if any coupon set $coupon_bgc='#eeeeee'; if($x_coupon_code) { //-->@mod Sep 14, 2023 Added One-Time Coupons support. //$q_coupon="select * from coupons where (coupon_code='$x_coupon_code') && (to_days(start_date) <=to_days(now())) && (to_days(expire_date) > to_days(now()))"; $q_coupon="SELECT * from coupons WHERE (coupon_code = '$x_coupon_code') && ( (coupon_type = 4) || (to_days(start_date) <=to_days(now())) && (to_days(expire_date) > to_days(now())) )"; $r_coupon=mysql_query($q_coupon); if(($r_coupon) && mysql_num_rows($r_coupon)) { $coupon_info=mysql_fetch_assoc($r_coupon); $coupon_type=$coupon_info[coupon_type]; $coupon_perc=$coupon_info[coupon_perc]; $c_perc=($coupon_perc*.01); if($coupon_type == 1) { //--> any item (global) $coupon_item='your order'; $coupon_item_id=0; } elseif($coupon_type == 4) { //-->@One-Time any item (global) $coupon_item='your order, (One-Time-Use) '; $coupon_item_id=0; } elseif($coupon_type == 2) { //--> get item name $coupon_item_id=$coupon_info[ItemID]; $q_item="select ItemName from dd_catalog where ItemID='$coupon_item_id'"; $r_item=mysql_query($q_item); $coupon_item=''; if(($r_item) && mysql_num_rows($r_item)) { $a_item=mysql_fetch_assoc($r_item); $coupon_item=trim($a_item[ItemName]); } // end if } // end if, coupon type //--> make coupon description $c_desc=$coupon_perc.'% OFF of ('.$coupon_item.')'; $coupon_desc='

Code:'.$x_coupon_code.'
'.$coupon_perc.'% OFF of ('.$coupon_item.')

'; $coupon_bgc='#eeeeff'; } else { $coupon_desc=''; } // end if, query } // end if, any coupon code //--> make coupon table $coupon_table='
'.$coupon_desc.' Enter Coupon Code:
1 coupon per order
'; //------------------------------------ $col = "white"; //-->@order items table $prv_shipping_group=-1; $bulk_items_count=0; // how many bulk shipping items in cart $total_bulk_grams=0; $total_group_units=0; while($a1 = mysql_fetch_assoc($r1)) { $item_name=$a1['ItemName']; $weight=$a1['weight']; $weight_units=$a1['weight_units']; $item_qty=intval($a1['ItemQty']); $shipping_group=intval($a1['shipping_group']); $group_units=intval($a1['group_units']); if($shipping_group == ENUM_SGROUP_VHB) { $total_vhb_group_units+=($item_qty*$group_units); } // end if, ENUM_SGROUP_VHB else if( ($shipping_group == ENUM_SGROUP_BULK) && ($weight_units == 'g') ) { $bulk_items_count++; // count bulk items count $total_bulk_grams+=($item_qty*$weight); // add weight in grams } // end if, ENUM_SGROUP_BULK //-->@Shipping Group row /* ENUM_SGROUP_FREE 0 ENUM_SGROUP_REGULAR 0 ENUM_SGROUP_VHB 1 ENUM_SGROUP_BULK 2 */ if( ($prv_shipping_group != $shipping_group) ) { $prv_shipping_group=$shipping_group; // update to current shipping group if($shipping_group == ENUM_SGROUP_BULK) { $rows.=' Bulk Shipping Items '; } // end if ENUM_SGROUP_BULK if($shipping_group == ENUM_SGROUP_VHB) { $rows.=' VHB Items (Virtual Herb Bags) x#VHB-BAGS# Current VHB(#VHB-CUR_UNITS# / 8) each VHB can hold (8) Units '; } // end if ENUM_SGROUP_VHB else if($shipping_group == ENUM_SGROUP_FREE) { $rows.=' FREE Shipping Items '; } // end if ENUM_SGROUP_REGULAR } // end if, new shipping_group $prodType=$a1['prodType']; $priceStruct=$a1['priceStruct']; switch($priceStruct) { case 0: // single payment $price=$a1['ItemPrice']; break; case 1: // subscription break; case 2: // installment plan $price=$a1['plItemPrice']; break; default: } // end switch, check price structure //--> check if item is discounted $item_id=$a1['ItemID']; //-->@mod Sep 14, 2023 Added One-Time Coupons support. if(($coupon_type == 1) || ($coupon_type == 4) || (($coupon_type == 2) && ($item_id == $coupon_item_id))) { $item_discount=round( ($a1['ItemTotal'])-($a1['ItemTotal']*$c_perc),2); //--> money saved for item total via coupon $coupon_discount+=($a1['ItemTotal']-$item_discount); //--> adjusted item total after removing discount percentage $item_total=$item_discount; } else { $item_total=$a1['ItemTotal']; } // end if //--> item image $img_url=$a1['ItemImageURL']; if($img_url) { //--> get fixed width image //--> don't force up-scale if width is less than target_width $img_tag=fixedWidthImage($img_url,80,0); } else { $img_tag=fixedWidthImage($site_url.'/images/no_image.gif',80); } // end if, item image //-->@item details //-->@get item details $item_details=get_price_struct_table($a1,true,true); // exclude price, already shown beside Quantity,show low inventory notice //--make item info $item_info='
'.$img_tag.'
'.$item_name.$item_details.'
'; if($col == "white") { $col = "dddddd"; } else { $col = "white"; } // end if, row color $rows .= "\n\t$item_info \n$aset[currency_sign] $price \t\n\t \n\t\t \n\t\t \n\t \n\t$aset[currency_sign] $a1[ItemTotal] \n $sub_tbl_row \n\n"; //--> add to sub total, price with no discount... $sub_total+=$a1[ItemTotal]; } // end while //-->@shipping cost $shipping_cost=0; // default zero //show VHB bags count $vhb_shipping_cost=0; if($total_vhb_group_units >0) { $max_vhb_units=8; // max VHB units per bag $base_vbh_shipping_cost=floatval( CART_BASE_VHB_SHIP_COST ); $vhb_full_bags=floor($total_vhb_group_units/$max_vhb_units); $vhb_bag_count=ceil($total_vhb_group_units/$max_vhb_units); $vhb_shipping_cost=($base_vbh_shipping_cost*$vhb_bag_count); //-->@current VHB bag Units if($total_vhb_group_units <= $max_vhb_units) { //-->@less than or 1 bag $vhb_cur_units_count=$total_vhb_group_units; } else { //-->@more than 1 bag, count remaining VHB units over $vhb_cur_units_count=$total_vhb_group_units-($vhb_full_bags*$max_vhb_units); } // end if, calc VHB current bag units //-->@replace VHB bags placeholder $rows=str_replace('#VHB-BAGS#',$vhb_bag_count,$rows); //-->@replace VHB current Units place holder $rows=str_replace('#VHB-CUR_UNITS#',$vhb_cur_units_count,$rows); } // end if, show VHB Bag Count if($vhb_shipping_cost >0) $shipping_cost+=$vhb_shipping_cost; //-->@bulk items count $bulk_shipping_cost=0; if($total_bulk_grams >0) { $threshold_bulk_units=13; // ounces needed before hitting extra bulk shipping cost threshold $base_bulk_shipping_cost=3; $extra_bulk_shipping_cost=0.20; $bulk_grams_oz=28; // 28 grams per ounce $bulk_grams_threshold=$bulk_grams_oz+($bulk_grams_oz*$max_bulk_units); $bulk_units_count_oz=floor($total_bulk_grams/$bulk_grams_threshold); if($bulk_items_count > 0) { $bulk_shipping_cost+=$base_bulk_shipping_cost; if($bulk_units_count_oz >= $threshold_bulk_units) { $rem_bulk_units_count_oz=($bulk_units_count_oz-$threshold_bulk_units); $bulk_shipping_cost+=($rem_bulk_units_count_oz*$extra_bulk_shipping_cost); } // end if, at or past bulk threshold in ounces //-->@add bulk shipping to shipping_cost $shipping_cost+=$bulk_shipping_cost; } // end if, calculate bulk shipping } //-->@format shipping cost $shipping_cost = number_format($shipping_cost, 2, ".", ","); //---- $sub_total = number_format($sub_total, 2, ".", ","); $order_total=$shipping_cost+($sub_total-$coupon_discount); //-->@format order total $order_total = number_format($order_total, 2, ".", ","); if($coupon_discount) { $coupon_discount ='

Coupon '.$x_coupon_code.' -'.$aset[currency_sign].number_format($coupon_discount, 2, ".", ",").'

'; } else { $coupon_discount=''; } // end if include_once('templates/HeaderTemplate.php'); include_once('templates/ViewCartTemplate.php'); } include_once('templates/FooterTemplate.php'); ?>