well its long but here goes...
Code: Select all
<?php
<?php
class Shipping
{
var $hazmat_message;
var $hazmat;
var $status;
var $h_weight;
var $real_weight;
var $zone;
// constructor - set variables
function Shipping($add1, $add2, $country, $state, $zip, $weight)
{
$this->add1 = $add1;
$this->add2 = $add2;
$this->country = $country;
$this->state = $state;
$this->zip = $zip;
$this->weight = $weight;
$this->usps_array = array("AA", "AE", "AK", "AP", "AS", "FM", "GU", "HI","MH", "MP", "PW", "VI", "PR");
$this->status = $this->getLocation();
$this->setWeight();
$this->setZone();
}
// METHODS ********************************************************************************
// STATUS CODES
// 1 = USA
// 2 = CANADA
// 3 = INTERNATIONAL
// ground shipping function
function ground()
{
if(($this->status == '1' || $this->status == '2') && !$this->checkPOBOX() && !$this->checkStates())
{
return $this->display('Standard Ground Shipping','FXG','').$this->hazmat_message;
}
}
// overnight shipping function
function overNight()
{
if($this->status == '1' && !$this->checkPOBOX() && !$this->checkStates() && !$this->hazmat)
{
$_SESSION['fed_1day'] = $this->getPrice($this->zone,'fed_1day',10);
return $this->display('Priority Overnight Shipping','FSO',$_SESSION['fed_1day']);
}
}
// 2 day express shipping
function fed2Day()
{
if($this->status == '1' && !$this->checkPOBOX() && !$this->checkStates() && !$this->hazmat)
{
$_SESSION['fed_2day'] = $this->getPrice($this->zone, 'fed_2day',10);
return $this->display('2 Day Shipping','FX2',$_SESSION['fed_2day']);
}
}
// 3 day express shipping
function fed3Day()
{
if($this->status == '1' && !$this->checkPOBOX() && !$this->checkStates() && !$this->hazmat)
{
$_SESSION['fed_3day'] = $this->getPrice($this->zone, 'fed_3day',10);
return $this->display('3 Day Shipping','FX3',$_SESSION['fed_3day']);
}
}
// International
function fedInt()
{
if(($this->status == '3' || $this->status == '2') && !$this->checkPOBOX() && !$this->checkStates() && !$this->hazmat)
{
$_SESSION['fed_int'] = $this->getPrice($this->zone, 'fed_int',3);
return $this->display('Priority International','FXI',$_SESSION['fed_int']);
}
}
// usps shipping function
function usps()
{
if(($this->checkPOBOX() || $this->checkStates()) && $this->status == '1' && !$this->hazmat)
{
$_SESSION['USPS'] = $this->getPrice($this->zone, 'fed_2day',-10);
return $this->display('USPS Priority','PIP',$_SESSION['USPS']);
}
}
// check to see if any errors apply to this order
function error()
{
if($this->status != '1' && $this->checkPOBOX())
{
echo '<font color="red"><B>ERROR:</B> PO BOXES are only issued in the United States, please re-check your information</font><BR><BR>';
}
if(($this->checkPOBOX() || $this->checkStates() || $this->status == '3') && $this->hazmat)
{
echo '<font color="red"><B>ERROR:</B> You have items in your cart that are listed as ORMD items, these cannot ship to PO BOXES or outside the Continental U.S. </font><BR><BR>';
}
if($this->checkPOBOX())
{
echo '<BR><font color="red"><B>PO BOXES:</B> We currently can only ship to PO BOXES via USPS Priority Shipping which is an additional charge. To avoid the additional charge please ship to a valid street address. </font><BR><BR>';
}
}
// ACCESSOR FUNCTIONS **********************************************************************
// determine based on the location which shipping options are available
function getLocation()
{
if($this->country == 'USA')
{
return '1';
}
elseif($this->country == 'CAN')
{
return '2';
} else {
return '3';
}
}
// display the appropriate radio buttons
function display($name,$value, $price)
{
if($price != '')
{
$price = ' + $'.$price;
}
if($value == $_SESSION['ship_method'])
{
$checked = 'checked';
}
return "<input class="radio" type="radio" name="ship_method" value="$value" $checked>$name$price<BR>";
}
// get the price of the shipping option
function getPrice($select, $from, $percent)
{
$result_ship = query_db("SELECT $select FROM $from WHERE weight = $this->h_weight");
$row_ship = @mysql_fetch_array($result_ship);
if($this->h_weight == 151 || $this->h_weight == 101){
$price = ($row_ship[$select]) * $this->weight;
} else {
$price = ($row_ship[$select]);
}
$fed_sur = sprintf("%01.2f", ($price / 100) * $percent);
$_SESSION[$from] = sprintf("%01.2f", ($fed_sur + $price));
return $_SESSION[$from];
}
// SET THE WEIGHT FOR PRICING
function setWeight()
{
if($this->country == 'USA')
{
if($this->weight > 150)
{
$this->h_weight = 151;
} else {
$this->h_weight = $this->weight;
}
} else {
if($this->weight > 100)
{
$this->h_weight = 101;
} else {
$this->h_weight = $this->weight;
}
}
} // end of setWeight function
// SET THE ZONE IF IN THE USA
function setZone()
{
if($this->status != 3)
{
$new_zip = substr($this->zip, 0,3);
$result = query_db("SELECT zone_num FROM zones WHERE $new_zip BETWEEN zip1 AND zip2");
$row = @mysql_fetch_array($result);
}
if($row){
$this->zone = ($row['zone_num']);
}
elseif($this->country == 'CAN')
{
$this->zone = 'A';
} else {
$result_c = query_db("SELECT countries_zone FROM countries WHERE countries_id = '$this->country'");
$row_c = @mysql_fetch_array($result_c);
$zone = ($row_c['countries_zone']);
$this->zone = $zone;
}
return $this->zone;
}
// CHECK IF USER IS SHIPPING TO A PO BOX
function checkPOBOX()
{
$pattern = array("box", "pbx", "bx");
foreach($pattern as $pat)
{
if (eregi($pat, $this->add1) || eregi($pat, $this->add2))
{
return TRUE;
}
}
}
// CHECK IF STATE IS FOR USPS
function checkStates()
{
if(in_array($this->state, $this->usps_array))
{
return TRUE;
}
}
// CHECK FOR HAZMAT FLAG
function checkHazmat($hazmat)
{
if($hazmat)
{
$this->hazmat = TRUE;
return TRUE;
}
}
// SET THE HAZMAT MESSAGE
function setHazmatMSG($message)
{
$this->hazmat_message = $message;
}
} // end of class
?>
?>