Zen Cart Logo
Forums / Built-in Shipping and Payment Modules / Changing shipping modules based on weight of the order?

Changing shipping modules based on weight of the order?

Locked

Views: 3,213

Results 1 to 18 of 18
This thread is locked. New replies are disabled.
28 Jul 2006, 7:13 PM
#1
tracyfloyd avatar

tracyfloyd

New Zenner

Join Date:
Mar 2006
Posts:
15
Plugin Contributions:
0

Changing shipping modules based on weight of the order?

For shipping, my client uses USPS unless the weight of the order is over 7lbs... then they use UPS instead. To simplify things, I'd like to only show one shipping module when you get to the checkout section... So, how can I have Zen Cart show only the USPS shipping module when the order is less than 7lbs. and then ONLY show the UPS module when the order is greater than 7lbs.?

30 Jul 2006, 5:16 AM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Changing shipping modules based on weight of the order?

Make a function to control the $this->enabled in the shipping modules so that they can test the weight and flip them on/off appropriately even though they are both installed ...

Note: make a duplicate function for the admin that just returns true ...

31 Jul 2006, 3:14 PM
#3
tracyfloyd avatar

tracyfloyd

New Zenner

Join Date:
Mar 2006
Posts:
15
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

OK thanks for pointing me in the right direction... I'll give it a shot!

31 Jul 2006, 5:23 PM
#4
tracyfloyd avatar

tracyfloyd

New Zenner

Join Date:
Mar 2006
Posts:
15
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

OK, I made the following changes and it seems to be working... let me know if you see any holes in my code or logic...

In the file: includes/modules/shipping/ups.php
AFTER:
if (zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
}

ADDED THIS:
$total_pounds = $this->pounds;
if ($total_pounds < 7) { $this->enabled = false; }

In the file: includes/modules/shipping/usps.php
AFTER:
if (zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
}

ADDED THIS:
$total_pounds = $this->pounds;
if ($total_pounds > 6) { $this->enabled = false; }

1 Aug 2006, 4:18 AM
#5
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Changing shipping modules based on weight of the order?

That's the basic idea ...

Controlling which shipping shows is nothing more than controlling that setting ...

Providing your test can be done at that moment, then you should have no problems ...

Be sure to test it under several circumstances ...

2 Aug 2006, 6:47 PM
#6
tracyfloyd avatar

tracyfloyd

New Zenner

Join Date:
Mar 2006
Posts:
15
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

OK, scratch what I used earlier... this seems to work mo' better:
Here I'm just adding up the weights of all the products in in the $order object and then using an if statement to toggle the shipping modules on or off baased on that total weight number...

In the file: includes/modules/shipping/ups.php
AFTER:
if (zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
}

ADDED THIS:
$order_weight_total = 0;
foreach ($order->products as $k => $v) {
$this_item = 0;
$this_item = $v['weight'] * $v['qty'];
$order_weight_total = ($order_weight_total + $this_item);
}
if ($order_weight_total < 7) { $this->enabled = false; }

In the file: includes/modules/shipping/usps.php
AFTER:
if (zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
}

ADDED THIS:
$order_weight_total = 0;
foreach ($order->products as $k => $v) {
$this_item = 0;
$this_item = $v['weight'] * $v['qty'];
$order_weight_total = ($order_weight_total + $this_item);
}
if ($order_weight_total > 6) { $this->enabled = false; }

3 Aug 2006, 2:32 AM
#7
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Changing shipping modules based on weight of the order?

The true weight of the order is available with: $_SESSION['cart']->show_weight()

However, where the $this->enable exists you cannot use the $_SESSION['cart']->show_weight() as the Admin also calls that portion of the module and the cart is not available in the Admin ...

This is where the function is handy ...

In the Catalog, the function is returning the value based on the shipping module for if the weight is too much or too little to display ...

In the Admin, the same function is made to just return true, so it always is valid ...

That is so when you go to the Modules ... it can display what is or isn't installed ...

The reason to use a function for this and the already existing $_SESSION['cart']->show_weight() is so that you also take into account attribute weight etc.

Now you can use it as you wrote it ... but be aware you are recalculating without taking into account things like Virtual Products, Downloads, Attributes etc. and their effect on weight of the order ...

Do a search in the Developer's Tool Kit for:
function zen_get_shipping_enabled

In both the Catalog/Admin to see how that function is used to control the $this->enable ... it might help you with a more accurate test ...

3 Sep 2006, 10:54 AM
#8
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

Ajeh:

The true weight of the order is available with: $_SESSION['cart']->show_weight()

However, where the $this->enable exists you cannot use the $_SESSION['cart']->show_weight() as the Admin also calls that portion of the module and the cart is not available in the Admin ...

This is where the function is handy ...

In the Catalog, the function is returning the value based on the shipping module for if the weight is too much or too little to display ...

In the Admin, the same function is made to just return true, so it always is valid ...

That is so when you go to the Modules ... it can display what is or isn't installed ...

The reason to use a function for this and the already existing $_SESSION['cart']->show_weight() is so that you also take into account attribute weight etc.

Now you can use it as you wrote it ... but be aware you are recalculating without taking into account things like Virtual Products, Downloads, Attributes etc. and their effect on weight of the order ...

Do a search in the Developer's Tool Kit for:
function zen_get_shipping_enabled

In both the Catalog/Admin to see how that function is used to control the $this->enable ... it might help you with a more accurate test ...

Well, I'm actually wondering how this all worked out really because I have the same issue almost exactly...

Truthfully wondering, Tracyfloyd, if you heeded Ajeh's advice as stated above and if the shipping module alterations seem to work for you? And if you did heed Ajeh's advice and it seemed to work for you then what exactly did you do to the corresponding files?

Thanks for any response in advance...

Sam
http://www.oldwestgames.com

7 Sep 2006, 2:10 AM
#9
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

okay,
I am still hoping to get some more help with this.
Ajeh, you have stated that tracyfloyd's modifications are likely to be inaccurate...
so I am hoping that someone can give me a modification that can work properly.
I am not versed in PHP so I will need some guidance here.

thanks
Sam

7 Sep 2006, 3:53 AM
#10
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

mafiasam:

okay,
I am still hoping to get some more help with this.
Ajeh, you have stated that tracyfloyd's modifications are likely to be inaccurate...
so I am hoping that someone can give me a modification that can work properly.
I am not versed in PHP so I will need some guidance here.

thanks
Sam

oh yeah...

Very little knowledge of PHP.
my zencart version is 1.3.0.2
I am using the future zen template.
I have activated the USPS module in production mode but have to wait until morning to have them move me to the production server as they are now closed.
I have activated the UPS module with $4 shipping and handling fee in lieu of the tare in the shipping/handling settings.

7 Sep 2006, 7:33 PM
#11
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

Basically here's where I'm at...

I have a dropshipper who basically uses these guidelines...

under 2 lbs the option of USPS or UPS is available to the customer
over 2 lbs only UPS is available to the customer

Using tracyfloyd's code for USPS as posted above and changing the 6 lb limit to a 2 lb limit seemed to work for the product itself, but when packaging is a consideration (for instance, a product that totals 1.54 lbs without packaging but totals 3.69 lbs with the tare configured) the option for USPS still shows in checkout.

That seems to be the only problem I have at this point. Here is the code I added to the USPS shipping module

$order_weight_total = 0;
foreach ($order->products as $k => $v) {
$this_item = 0;
$this_item = $v['weight'] * $v['qty'];
$order_weight_total = ($order_weight_total + $this_item);
}
if ($order_weight_total > 2) { $this->enabled = false; }

Does anyone have an idea what I can do to remedy this problem??????????

sam

(p.s. - contacted USPS and had my user id moved to the production server, so that is all good)

7 Sep 2006, 9:25 PM
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Changing shipping modules based on weight of the order?

let's combine the ideas:

    if (zen_get_shipping_enabled($this->code)) {
      $this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
    }

    if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) {
      $check_flag = false;
      $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_USPS_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
      while (!$check->EOF) {
        if ($check->fields['zone_id'] < 1) {
          $check_flag = true;
          break;
        } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
          $check_flag = true;
          break;
        }
        $check->MoveNext();
      }

      if ($check_flag == false) {
        $this->enabled = false;
      }
    }

[B]    // disable module if weight is below 2 lbs:
    if (IS_ADMIN_FLAG === false) {
      if ($_SESSION['cart']->show_weight() > 2) $this->enabled = false;
    }[/B]
```Basically, all I'm suggesting is the last 3 lines here.
This combines Ajeh's warning about being in admin or not, and if safe, grabs the order's weight details ... then compares it, and if > 2 then disables the module.
8 Sep 2006, 12:33 AM
#13
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

DrByte:

let's combine the ideas:

if (zen_get_shipping_enabled($this->code)) {
  $this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
}

if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) {
  $check_flag = false;
  $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_USPS_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
  while (!$check->EOF) {
    if ($check->fields['zone_id'] < 1) {
      $check_flag = true;
      break;
    } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
      $check_flag = true;
      break;
    }
    $check->MoveNext();
  }

  if ($check_flag == false) {
    $this->enabled = false;
  }
}

[B] // disable module if weight is below 2 lbs:
if (IS_ADMIN_FLAG === false) {
if ($_SESSION['cart']->show_weight() > 2) $this->enabled = false;
}[/B]

> This combines Ajeh's warning about being in admin or not, and if safe, grabs the order's weight details ... then compares it, and if > 2 then disables the module.


I applied the code however it still wants to bring up USPS Priority Mail for some reason. My dropshipper only offers first class and priority for items under 2 lbs. As you can see from the example image below

<http://www.oldwestgames.com/images/shipping_example.jpg>

it is still showing the USPS priority mail option.
8 Sep 2006, 12:45 AM
#14
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

until a solution is found I am simply going to disable USPS Priority so that the option is not available but I would prefer to have this option available to sutomers ordering lightweight products.

8 Sep 2006, 3:12 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Changing shipping modules based on weight of the order?

Hmmm ... I just tested on a fresh v1.3.0.2 and v1.3.5 install, making ONLY the code-suggestion I posted above, and it works great: USPS only shows up if the weight of the entire order is less than 2. If it's more than 2, then I only see the details for the other shipping modules.

USPS options when it DOES show, are related to whatever checkboxes are activated in the USPS module in the admin. Defaults may include Express mail, priority mail, parcel post, media mail, etc etc etc. They are only limited to Priority if you have all the other options disabled or if only Priority is offered by USPS for that particular shipment.

14 Sep 2006, 1:39 PM
#16
nomaad avatar

nomaad

New Zenner

Join Date:
Apr 2006
Posts:
13
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

I have tried this method in 1.2.6 and get an errors after checkout. The order goes through OK but no confirmation page. Only errors . . . Any ideas?

Which I am trying to use it with UPS and MZMT. My client is a greenhouse distributor and wants accessories shipped UPS as they are small but needs the greenhouses charged for Freight line shipping, hence the MZMT.

Any help would be greatly appreciated.

14 Sep 2006, 3:51 PM
#17
mafiasam avatar

mafiasam

Zen Follower

Join Date:
Jan 2006
Location:
Portland, Oregon
Posts:
256
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

DrByte:

Hmmm ... I just tested on a fresh v1.3.0.2 and v1.3.5 install, making ONLY the code-suggestion I posted above, and it works great: USPS only shows up if the weight of the entire order is less than 2. If it's more than 2, then I only see the details for the other shipping modules.

USPS options when it DOES show, are related to whatever checkboxes are activated in the USPS module in the admin. Defaults may include Express mail, priority mail, parcel post, media mail, etc etc etc. They are only limited to Priority if you have all the other options disabled or if only Priority is offered by USPS for that particular shipment.

But that's the problem. I don't want Priority showing for anything over 2 pounds either. First class can only be used for items under 13 ounces anyway so I wouldn't have a problem there really. It's the Priority shipping that's a problem really.

My supplier actually has a flat rate for USPS first class and USPS Priority. $4.95 and $9.95 respectively. I have not seen a way or solution in Zen Cart to configure the setup that way.

14 Sep 2006, 5:01 PM
#18
nomaad avatar

nomaad

New Zenner

Join Date:
Apr 2006
Posts:
13
Plugin Contributions:
0

Re: Changing shipping modules based on weight of the order?

Alright, in 1.2.6 the only way I could get the shipping to predictably handle the weights is by adding the following:

AFTER THIS:
if (zen_get_shipping_enabled($this->code)) {

    $this->enabled = ((MODULE_SHIPPING_UPS_STATUS == 'True') ? true : false); 

}

ADD THIS (changing your weight):
if ($_SESSION['cart']->show_weight() > 241) { $this->enabled = false; }

THEN IN MZMT (or other) AFTER THIS:
if (zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_MZMT_STATUS == 'True') ? true : false);
}

ADD THIS (changing your weight):
if ($_SESSION['cart']->show_weight() < 241) { $this->enabled = false; }

Seems to work out fine for me, I hope helps out.