I think I have got along way towards getting this sorted and just need a bit of assistance with the last bit that I am stumped on and am fairly sure it is just a matter of code in wrong place.
The code below works well except that the $rate variable is always at the international rate so is not picking up if the user is domestic. I can echo country and it comes up correctly and matches what is in the config file.
Any help appreciated.
Cheers
class item_ship
{
var $classname = "item_ship";
function list_rates( &$d ) {
global $total, $tax_total, $CURRENCY_DISPLAY;
$db =& new ps_DB;
$dbv =& new ps_DB;
$cart = $_SESSION['cart'];
/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
if ( $_SESSION['auth']['show_price_including_tax'] != 1 ) {
$taxrate = 1;
$order_total = $total + $tax_total;
}
else
{
$taxrate = $this->get_tax_rate() + 1;
$order_total = $total;
}
/* Get Shipping/Billing Country from Database */
$database = new ps_DB();
$q = "SELECT country FROM #__{vm}_user_info WHERE user_info_id='".$d['ship_to_info_id']."'" ;
$database->query($q);
$database->next_record();
$country = $database->f("country");
$rate = $this->get_rate($d);
$shipping_rate_id = urlencode($this->classname."|STD|Standard Shipping|" . $rate . '|' . $rate);
$html = "Rate $rate";
$html .= "\n<input type=\"radio\" name=\"shipping_rate_id\" checked=\"checked\" value=\"$shipping_rate_id\" />\n";
$html .= "Standard Shipping: ".$CURRENCY_DISPLAY->getFullValue($rate);
$_SESSION[$shipping_rate_id] = 1;
echo $html;
return true;
}
function get_rate( &$d )
{
$db =& new ps_DB;
$total = 0.00;
foreach($_SESSION['cart'] as $key=>$value)
{
//Query for that products shipping amount
//Multiply by quantity and add to sum
if ($key !== "idx")
{
if ( $country == FLEX2_SHIPPING_COUNTRY )
{
$product_id = $value['product_id'];
$quantity = $value['quantity'];
$sql = "Select amount from #__{vm}_product_shipping, ".
"#__{vm}_product where #__{vm}_product.product_sku = " .
"#__{vm}_product_shipping.product_sku and #__{vm}_product.product_id="
. $product_id;
$db->query($sql);
$db->next_record();
$shipping_amt = $db->f('amount');
$total += round(($shipping_amt*$quantity), 2);
}
else
{
$product_id = $value['product_id'];
$quantity = $value['quantity'];
$sql = "Select i_amount from #__{vm}_product_shipping, ".
"#__{vm}_product where #__{vm}_product.product_sku = " .
"#__{vm}_product_shipping.product_sku and #__{vm}_product.product_id="
. $product_id;
$db->query($sql);
$db->next_record();
$shipping_amt = $db->f('i_amount');
$total += round(($shipping_amt*$quantity), 2);
}
}
}
return $total;
}
function get_tax_rate()
{
/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
if( intval(SHIPPRODUCT_TAX_CLASS)== 0 )
return( 0 );
else {
require_once( CLASSPATH. "ps_tax.php" );
$tax_rate = ps_tax::get_taxrate_by_id( intval(SHIPPRODUCT_TAX_CLASS) );
return $tax_rate;
}
}
/* Validate this Shipping method by checking if the SESSION contains the key
* @returns boolean False when the Shipping method is not in the SESSION
*/
function validate( $d ) {
$shipping_rate_id = $d["shipping_rate_id"];
if( array_key_exists( $shipping_rate_id, $_SESSION ))
return true;
else
return false;
}
/**
* Show all configuration parameters for this Shipping method
* @returns boolean False when the Shipping method has no configration
*/
function show_configuration()
{
$db =& new ps_DB;
global $PHPSHOP_LANG;
/** Read current Configuration ***/
require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
// Determine if the appropriate configuration table exists
// if not, create it
$sql = "Select product_sku, amount, i_amount from #__{vm}_product_shipping";
$db->query($sql);
if (!$db->next_record())
{
//There is no current configuration or the table doesn't exist
//Therefore, we will create an initial table
$sql = "CREATE TABLE `#__{vm}_product_shipping` " .
"(`product_sku` varchar(64) NOT NULL, `amount` float NOT NULL, `i_amount` float NOT NULL, " .
"PRIMARY KEY (`product_sku`) )";
$db->query($sql);
echo "No records";
}
/** Read current Configuration ***/
echo "<table>";
$sql = "Select product_sku, amount, i_amount from #__{vm}_product_shipping";
$db->query($sql);
while ($db->next_record())
{
echo '<tr><td>Shipping Amount for ' .
$db->f('product_sku') .
'</td><td><input type="text" size="4" name="' .
$db->f('product_sku') .
'" value="' .
$db->f('amount') .
'"></td><td><input type="text" size="4" name="' .
$db->f('product_sku') .
'" value="' .
$db->f('i_amount') .
'"></td>' .
'<td>' . vmToolTip("Enter a new amount, or leave the amount blank to remove a value shipping record") . '</td>' .
'</tr>';
}
//display a box to input new entries
echo '<tr><td>New Product: <input type="text" name="__new_product" size="20"></td>';
echo '<td>Domestic Amount: <input type="text" name="__new_product_amount" size="4"></td>';
echo '<td>International Amount: <input type="text" name="__new_product_i_amount" size="4"></td>';
echo '<td>' . vmToolTip("Enter a product SKU then domestic amount then international amount to create a new value shipping record") . '</td>';
echo '</tr>';
?>
<tr>
<td><strong><?php echo $PHPSHOP_LANG->_PHPSHOP_UPS_TAX_CLASS ?></strong></td>
<td>
<?php
require_once(CLASSPATH.'ps_tax.php');
ps_tax::list_tax_value("SHIPPRODUCT_TAX_CLASS", SHIPPRODUCT_TAX_CLASS) ?>
</td>
<td><?php echo vmToolTip("Use the following tax class on the shipping charge. The shipping charge values above will then be inclusive of this tax rate.") ?><td>
</tr>
<?php
echo "</table>";
return true;
}
/**
* Returns the "is_writeable" status of the configuration file
* @param void
* @returns boolean True when the configuration file is writeable, false when not
*/
function configfile_writeable()
{
return is_writeable( CLASSPATH."shipping/".$this->classname.".cfg.php" );
}
/**
* Writes the configuration file for this shipping method
* @param array An array of objects
* @returns boolean True when writing was successful
*/
function write_configuration( &$d )
{
//if there is a new item, insert it:
$db =& new ps_DB;
if (isset($d['__new_product']) &&
isset($d['__new_product_amount']) &&
isset($d['__new_product_i_amount']) &&
$d['__new_product'] != '' &&
$d['__new_product_amount'] != '' &&
$d['__new_product_i_amount'] != '')
{
$product = $d['__new_product'];
$amount = round(floatval($d['__new_product_amount']), 2);
$i_amount = round(floatval($d['__new_product_i_amount']), 2);
$sql = "Insert into #__{vm}_product_shipping values(" .
"'$product', '$amount', '$i_amount')";
$db->query($sql);
}
//lookup all existing records, iterate through items and
//update as necassary, removing items whose price is now blank or null
$sql = "Select product_sku, amount from #__{vm}_product_shipping";
$db->query($sql);
$sqlstmts = array();
while ($db->next_record())
{
$product_sku = $db->f('product_sku');
$amount = $db->f('amount');
//first case, we have a changed shipping amount
if (isset($d[$product_sku]) &&
$d[$product_sku] != '' && $d[$product_sku] != $amount)
{
$new_amount = round(floatval($d[$product_sku]),2);
$sqlstmts[] = "Update #__{vm}_product_shipping set amount = $new_amount ".
"where product_sku = '$product_sku'";
}
//second case, we have an existing product that has been blanked
elseif (isset($d[$product_sku]) && $d[$product_sku] == '')
{
$sqlstmts[] = "Delete from #__{vm}_product_shipping ".
"where product_sku = '$product_sku'";
}
}
foreach($sqlstmts as $sql)
{
$db->query($sql);
}
$my_config_array = array(
"SHIPPRODUCT_TAX_CLASS" => $d['SHIPPRODUCT_TAX_CLASS']
);
$config = "<?php\n";
$config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
foreach( $my_config_array as $key => $value )
{
$config .= "define ('$key', '$value');\n";
}
$config .= "?>";
if ($fp = fopen(CLASSPATH ."shipping/".$this->classname.".cfg.php", "w"))
{
fputs($fp, $config, strlen($config));
fclose ($fp);
return true;
}
else
{
$vmLogger->err( "Error writing to configuration file" );
return true;
}
}
}
?>
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313:





by: kennedypd1Posted on 2009-06-16 at 06:02:34ID: 24637468
I have found a couple of modules that work fine and if someone can please have a look at what I am doing wrong here it would be appreciated.
I have found this code that lets me bill per item based on SKU.
I works fine except I cant differentiate between domestic and international.
I have adatped the table for this code to have an extra field that is called i_amount that has the international freight charges per SKU and amont that has the domestic freight charges.
So I need to get the code in file 24637468a.txt (attached below) to differentiate the country of delivery.
The code in file 24637468b.txt (attached below) works well for doing domestic and international transactions so I need to cut the code from this one that works out the country of origin
Any help much appreciated
24637468b (second code) uploaded by Netminder 17 June 2009
24637468a (first code) uploaded by Netminder 17 June 2009