In the official osCommerce Forum, Paul asks;
… I need to prevent any customer that has not got a minimum total weight in their cart from proceeding through the checkout; does anyone have any ideas on how this might be achieved most efficiently? I don’t want to be hand-held through making a contribution to do this, I am quite capable of writing the code myself … I realise that I could do this with a shipping module, in fact I have already written three custom shipping modules specifically for this site into which I could insert the checks on the total cart weight, but I am thinking there may be a more efficient way to implement the functionality we require, without having to resort to checking the cart weight from within 3 separate shipping modules.
This is fairly straightforward and my answer was this (as he did not want the code delivered to him on a plate);
checkout_shipping.php … look for $total_weight and “if” it to a tep_redirect
Thanks for the reply; think I will hack it then … I was hoping I might stumble across a more elegant and reusable solution, but your suggestion will provide the required functionality with minimal changes.
So, more elegant and re-usable…this will make a good blog post for others to read, so let’s do it!
Step 1: Add the admin configuration
Best place for this is in admin > shipping/packaging
Running this piece of SQL code (in PHPMyAdmin) will add another entry in here:
[php]INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added)
VALUES (‘Enter the Minimum Weight you will ship’, ‘SHIPPING_MIN_WEIGHT’, ‘0’, ‘The minimum weight that you will ship. 0 = unlimited.’, ‘7’, ‘6’, now());[/php]
So that it now looks like this:
Step 2: Configure the minimum weight
Click on the “Enter the minimum weight you will ship” and press the  button. You should see something like this;
As you can see, you should leave this at zero if you wish to allow ANY weight to checkout. If you wish to allow weight above 10 (units of weight), then insert the number 10 here and press [update]. Easy.
Step 3: Add the “check weight” code
Open up checkout_shipping.php and find these lines of code:
[php]$total_weight = $cart->show_weight();
$total_count = $cart->count_contents();[/php]
Right underneath, add this line of code:
[php]if ((SHIPPING_MIN_WEIGHT > 0) && ($total_weight < SHIPPING_MIN_WEIGHT)) tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, 'error_message=' . urlencode(sprintf(ERROR_UNDERWEIGHT_SHIPPING, SHIPPING_MIN_WEIGHT, SHIPPING_MIN_WEIGHT-$total_weight)), 'NONSSL'));[/php] Save the file and upload it. I'll dissect that line of code later on in this blogpost. Step 4: Add the Error Message to the language file
Open up /includes/languages/english.php and add the following line of code:
[php]define(‘ERROR_UNDERWEIGHT_SHIPPING’, ‘The minimum shipping weight is %s kgs. You need to add another %s kgs to be able to checkout.’);[/php]
In this line of code I have assumed that your “unit of weight” that you use in your shop is Kilograms, hence the use of kgs. If you use ounces, change kgs to ounces, if you use grams, change it to grams. Easy enough? Save the file and upload it. Notice the %s used here? I’ll explain that later on.
That is all the changes that is needed.
And the result?
Assuming that your Minimum Shipping Weight is set above zero, then if the total weight of the cart contents is below this setting, an error message will be displayed, somewhat like this:
And if the Minimum Shipping weight is set to zero OR if the Minimum Shipping Weight has been exceeded, then checkout will simply progress as normal.
Wasn’t that easy? And so much quicker than;
a. asking if a contribution exists and waiting hours/days for a reply
b. installing the contribution if it does exist
c. bug testing and fixing the contribution if it’s broken
And, even better, you now know a bit about how osCommerce code works!
Dissecting the Code
[php]if ((SHIPPING_MIN_WEIGHT > 0) && ($total_weight < SHIPPING_MIN_WEIGHT))[/php] If the Minimum Shipping Weight is greater than Zero AND the total weight of the cart contents is less than the Minimum Shipping Weight, then; [php]tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, 'error_message=' . urlencode(sprintf(ERROR_UNDERWEIGHT_SHIPPING, SHIPPING_MIN_WEIGHT, SHIPPING_MIN_WEIGHT-$total_weight)), 'NONSSL'));[/php] Redirect the buyer back to the shopping cart page, and show an error message asking him/her to buy more stuff! This particular piece of code is interesting; [php]sprintf(ERROR_UNDERWEIGHT_SHIPPING, SHIPPING_MIN_WEIGHT, SHIPPING_MIN_WEIGHT-$total_weight)[/php] sprintf allows us to pass parameters to the language definition. If you remember the language definition contianed a couple of %s "commands"...these %s are what is passed through, so in this case, I am saying: Use the ERROR_UNDERWEIGHT_SHIPPING language definition, but pass to it; 1. the SHIPPING_MIN_WEIGHT setting (which is the admin setting above zero) 2. the SHIPPING_MIN_WEIGHT less the total weight of the cart. This allows us to show the buyer, how much more weight he needs to have in his cart to be able to checkout. Easy as 123?