2 replies [Last post]
PN
PN's picture
Offline
Joined: 03/01/2010
Juice: 21

I am trying to pull a shipping rate, from the database, for each item in a shopping cart based on weight and then summing these rates to calculate a shipping cost. I decided to use the module uc_tablequote to achieve this functionality, but I am having problems getting it to work. The original function from uc_tablequote is shown below:

function uc_tablequote_quote(&$form_state, $products, $details){
  $rate = 0;
  $items = uc_cart_get_contents();
  foreach ($items as $item) {
    if (variable_get('uc_tablequote_type','weight') == 'weight') {
      $total += $item->weight * $item->qty;
    }
    elseif (variable_get('uc_tablequote_type','weight') == 'order') {
      $total += $item->price * $item->qty;
    }
  }
  $result = db_query("SELECT * FROM {uc_tablequote}");
  while ($r = db_fetch_object($result)) {
    if($total <= $r->max && $total >= $r->min) {
      $rate = $r->rate;
    }
  }

I modified this function by moving the SQL query inside of the foreach loop as shown below. I then added $rate += $rate_table; to sum the rates.

function uc_tablequote_quote(&$form_state, $products, $details){
  $rate_table = 0;
  $items = uc_cart_get_contents();
  foreach ($items as $item) {
    if (variable_get('uc_tablequote_type','weight') == 'weight') {
     
        $result = db_query("SELECT * FROM {uc_tablequote}");
        while ($r = db_fetch_object($result)) {
          if($item->weight <= $r->max && $item->weight >= $r->min) {
            $rate_table = $r->rate;
          }
        } 
    }
       
    elseif (variable_get('uc_tablequote_type','weight') == 'order') {
   
        $result = db_query("SELECT * FROM {uc_tablequote}");
        while ($r = db_fetch_object($result)) {
          if($item_weight <= $r->max && $item_weight >= $r->min) {
            $rate_table = $r->rate;
          }
        }
    }
  $rate += $rate_table;
  }

I then tested the following scenarios.

1 x Can (weight per item = 2 lb)
Shipping cost: $5.28

1 x Bag (weight per item = 1 lb)
Shipping Cost: $5.01

which shows that it's working. However, when I change the quantity I get the same thing.

2 x Can (weight per item = 2 lb)
Shipping cost: $5.28

2 x Bag (weight per item = 1 lb)
Shipping Cost: $5.01

which signifies that the rates aren't being added per item. The shipping cost should be $10.56 and $10.02, respectively. Now if I combine two different products I get

1 x Can (weight per item = 2 lb)
1 x Bag (weight per item = 1 lb)
Shipping Cost: $10.29

which is correct. This tells me that the quantities are not being considered when calculating the shipping cost. Is there something I am missing on getting this to work? Any help would be much appreciated

Thank you

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3369
Re: Help with calculating shipping cost for each item in shoppin

You need to multiply by $item->qty, like in the original code.

<tr>.
PN
PN's picture
Offline
Joined: 03/01/2010
Juice: 21
Re: Re: Help with calculating shipping cost for each item in sho

Thank you TR. I don't know how I could have missed that. It's so simple. I know this is a little of topic, but do you know the difference between the variables $item and $product? I was wondering why $item is used in the foreach loop as opposed to $product. Do the values of $product get passed to $item when something is added to the shopping cart?

Thanks again