I am creating a shoppingcart and I am working on a shipping module that would calculate the shipping rate according to the customer's postcode in the Mysql database.
Basically, I search the postcode entered by the customer in the existing database, each postcode refers to a zone which have different prices. From there, I calculate the price.
The idea here is to have only certain postcodes we can deliver to, then the rest is not possible.
How do I display the message (in lightbox or other infobox) that the postcode is out of our range and therefore we cannot deliver the product? (+ link to homepage)
Code: Select all
<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
class postcodeshipping {
var $classname = "postcodeshipping";
function list_rates( &$d ) {
global $CURRENCY_DISPLAY, $total;
$db = new ps_DB;
// Pull postshipping Configuration
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
// USER HAS TO SPEND THE MIN TO GET THE FREE SHIPPING
if (PS_BASE_AMOUNT_ACTIVE == 'Y') {
if ($total < PS_BASE_AMOUNT) {
$payForShipping = true;
} else {
$payForShipping = false;
}
} else {
$payForShipping = true;
}
if ($payForShipping == true) {
// GET THE CURRENT ZIP / POSTCODE FROM THE SHIPPING ADDRESS
$q = "SELECT zip FROM #__{vm}_user_info WHERE ";
$q .= "user_info_id='". $d["ship_to_info_id"] . "'";
$db->query($q);
$db->next_record();
$postcode = $db->f("zip");
$selectPostcode = " SELECT pz.ps_zone_name, pz.ps_zone_cost, pz.ps_zone_cent_per_kilo, pc.ps_postcode
FROM #__{vm}_ps_zone_details as pz, #__{vm}_ps_postcode as pc
WHERE pc.ps_id_fk = pz.ps_zone_name
AND pc.ps_postcode = '".$postcode."'
GROUP BY pz.ps_zone_name";
$db->query($selectPostcode);
if ($db->num_rows() > 0) {
$db->next_record();
$currentPostcode = $db->f( 'ps_postcode' );
$currentZone = $db->f( 'ps_zone_name' );
$currentZoneCost = $db->f( 'ps_zone_cost' );
$currentCostPerKilo = $db->f( 'ps_zone_cent_per_kilo' );
// EACH ITEM TOTAL WEIGHT += WEIGHT * QUANTITY
$numOfArray = count($_SESSION['cart']);
for ($i=0;$i<$numOfArray;$i++) {
if (trim($_SESSION['cart'][$i]['product_id']) != '') {
$selectWeightQty = "SELECT product_weight FROM #__{vm}_product WHERE product_id = '".$_SESSION['cart'][$i]['product_id']."'";
$db->query($selectWeightQty);
if ($db->num_rows() > 0) {
$weight_qty_total += ($db->f( 'product_weight' ) * $_SESSION['cart'][$i]['quantity']);
}
}
}
// FULL FORMULA FIXED RATE + (TOTAL WEIGHT * COST PER KILO)
$total_shipping_cost = number_format($currentZoneCost + ($weight_qty_total * $currentCostPerKilo),2);
$total_shipping_cost = number_format($total_shipping_cost,2);
//handling fee
$handling_fee = $GLOBALS['CURRENCY']->convert( $total_shipping_cost );
// THE ORDER OF THOSE VALUES IS IMPORTANT:
// carrier_name|rate_name|totalshippingcosts|rate_id
$shipping_rate_id = urlencode(__CLASS__."|STD|Shipping To The Postcode ".$currentPostcode." ".$total_shipping_cost."|".$handling_fee);
$html = "";
$html .= "\n<input type=\"radio\" name=\"shipping_rate_id\" id=\"flex_shipping_rate\" checked=\"checked\" value=\"$shipping_rate_id\" />\n";
$html .= "Shipping this item to the address ". $currentPostcode." will cost you: ";
$html .= $CURRENCY_DISPLAY->getFullValue($total_shipping_cost);
$_SESSION[$shipping_rate_id] = 1;
echo $html;
return true;
} elseif (POSTCODE_ERROR_ACTIVE != 'Y') {
$string = POSTCODE_ERROR;
echo $string;
print "Hello World!\n";
}
} else {
global $vendor_name, $VM_LANG;
$html = "<strong>".$VM_LANG->_('PHPSHOP_FREE_SHIPPING_CUSTOMER_TEXT')."</strong><br/>\n";
$html .= "<input type=\"hidden\" name=\"shipping_rate_id\" value=\"free_shipping|$vendor_name|".$VM_LANG->_('PHPSHOP_FREE_SHIPPING')."|0|1\" />";
echo $html;
return true;
}
}
// -------------------------------------------------------------------------------------------------------------------------------------------- get_rate
function get_rate( &$d ) {
// Parse Values from Shipment String
$shipping_rate_id = $d["shipping_rate_id"];
$is_arr = explode("|", urldecode(urldecode($shipping_rate_id)) );
$order_shipping = $is_arr[3];
return $order_shipping;
}
// -------------------------------------------------------------------------------------------------------------------------------------------- get_tax_rate
function get_tax_rate() {
return false;
}
// -------------------------------------------------------------------------------------------------------------------------------------------- validate
function validate( $d ) {
$shipping_rate_id = $d["shipping_rate_id"];
if( array_key_exists( $shipping_rate_id, $_SESSION ))
return true;
else
return false;
}
?>