Ubercart - Flatrate shipping hack Part 3
by admin on Jun.07, 2009, under Linux Blerg
Somehow the previous hack got broken, so I did a little fixing in the function hack
function uc_flatrate_quote($products, $details, $method) {
$method = explode('_', $method['id']);
$mid = $method[1];
$context = array(
'revision' => 'altered',
'location' => 'shipping-flatrate-method-json',
);
if ($method = db_fetch_object(db_query("SELECT * FROM {uc_flatrate_methods} WHERE mid = %d", $mid))) {
$context['subject']['flatrate_method'] = $method;
// Start at the base rate.
$rate = $method->base_rate;
foreach ($products as $product) {
$context['subject']['order_product'] = $product;
if (empty($product->flatrate) || is_null($product->flatrate[$mid])) {
$price_info = array(
'price' => $method->product_rate,
'qty' => $product->qty,
);
$product_weightz += $product->weight * $product->qty;
// Add the method's default product rate.
}
else {
$price_info = array(
'price' => $product->flatrate[$mid],
'qty' => $product->qty,
);
// Add the product-specific rate.
}
}
// CODE FOR CALCULATING SHIPPING PRICE BASED ON WEIGHT AND FLAT RATE SHIPPING //
// DECLARATION FOR RECEIVING THE TOTAL PRODUCT WEIGHT FROM CART CONTENT //
$items = uc_cart_get_contents();
foreach($items as $item) {
// CONVERT THE WEIGHT TO KG FOR SANE PRODUCT TOTAL KG //
switch ($item->weight_units) {
case 'g':
$weight_type = '0.001';
case 'kg':
$weight_type = '1';
case 'lb':
$weight_type = '0.45359237';
break;
case 'oz':
$weight_type = '0.028349523';
break;
}
// END CONVERT PRODUCT WEIGHT
$weightz += ($item->qty * $item->weight * $weight_type); // SOMEHOW IT'S NOT WORKING
}
$total_weight += $product_weightz; // GET WEIGHT DIRECTLY FROM MODULE FUNCTION
// $total_weight += $weightz; // SOMEHOW NOT WORKING NOW?
// IF THE TOTAL WEIGHT IS BELOW 0.5 KG THEN NO ADDITIONAL CHARGE ASIDE FROM THE BASE
// CHARGE
if ( $total_weight < = '0.5' ) {
$rate += '0';
}
// ADDITIONAL CHARGE PER KG, ALL DECIMAL IS ROUNDED UP AND MULTIPLY BY THE
// product_rate SET ON THE FLAT RATE MODULE
if ( $total_weight > '0.5' ) {
$rate += $method->product_rate * ceil($total_weight);
}
// FINISH THE ADDITIONAL CODE FOR WEIGHT CALCULATION //
unset($context['subject']['order_product']);
$altered = uc_price($rate, $context);
$context['revision'] = 'formatted';
$formatted = uc_price($rate, $context);
$quotes[] = array('rate' => $altered, 'format' => $formatted, 'option_label' => check_plain($method->label));
}
return $quotes;
}
-= end transmission =-


















June 10th, 2009 on 9:59 pm
I want to find good pop music. Help me please.
November 13th, 2009 on 8:06 pm
Yes this seems to work well. I just changed round to ceil.. as that works better in my case. Cool script, I’ll mention this when I do a case study.