How To (Ubercart): Create a shipping "weight limit" message to the shopper

Problem
A client requested to show an error message on the /cart and cart/checkout page when the order shipping weight is greater than 500lbs. They are shipping grass seed and need the purchaser to complete the order over the phone.
Options
I did try using workflow-ng first but with this ubercart install some of the workflow options were not in place. I love workflow but I don't dig to deep into the code. I wanted another solution.
You could create a custom module and check the total weight when on /cart and /cart/checkout too. If this was my personal site this is the solution I would of selected.
Another option is more basic. Create a block and use PHP to do the work. This is the option I choose. If the client has a need for a custom module to override other module stuff then we can always move this solution to the module.
SolutionFirst you need to create a block and set it up to only show up on

  • cart
  • cart/checkout
  • cart/checkout/*

There is no need for a block title, so disregard that. Once done, for the block body you will use the following code. Be sure to choose PHP for the input format!

<?php
$items = uc_cart_get_contents();
foreach($items as $item) {
$weights += ($item->qty * $item->weight);
}
$total_weight = $weights;

if($total_weight > '500') {
drupal_set_message("Your message here...", 'error');
}
?>

Now click save and give it a test run.
Enjoy!

Comments

Hey Elvis, Clever and quick solution that leverages Drupal's block capabilities! We have a similar situation to your client as we sell bulk soybean candle wax in quanties anywhere from 50-pound boxes to shipping containers. Our eCommerce site mostly sells various quantities of 50-pound boxes to individuals and small businesses. At 50 pounds per box, the order doesn't need to be that large to hit the fine line where motor freight can be explored as an option to UPS. As you point out, this quick solution is useful until there is a need for further site-specific customization. Thanks for sharing this idea. --SoyaWax Jim-- www.SoyaWaxInternational.com

<p>@SoyaWaxJim, Hi, I did end up putting this code into a custom module for the client, as they wanted to remove the "checkout" buttons and prevent the user from checking out.</p> <p>What solution did you use for your issue?</p>

Hey, again, Elvis, Actually, we are up to our ears in alligators still, thanks to the historic flood in Cedar Rapids this summer. So I split my time between our trying to physically rebuild our business and moving things forward... like improving our Drupal/Ubercart-based eCommerce store for "low-volume customers" (AKA individuals and small businesses). So in all honesty I'll be using your quickie (and temporary) solution here before tackling more broad and forward-looking growth of our business. The thing I like about your idea here is that it is a good example of how to leverage the flexible block capabilities of Drupal. Long-term I have some custom modules under development that will shape our on-line eCommerce business model. In the short-term, however, it's flood recovery that consumes too much of our time and energy. Thanks, again, for sharing this idea. And keep up the good work of sharing your Drupal experience and ideas. --SoyaWax Jim-- www.SoyaWaxInternational.com

<p>Elvis, so far I used your block on a dev site. Works pretty good. Would love to try out your module if your willing to share. Thanks, redhatmatt</p>

<p>Glad to hear others getting some use out of it. I will check that on that module the next time I am doing some work for the client.</p> <p>Regards</p>

Add new comment