Converting ZenCart Product Weight from Ounces to Pounds

Written by:

Converting ZenCart Product Weight from Ounces to Pounds
  • 0.00 / 5 5
0 votes, 0.00 avg. rating (0% score)

Current versions of Zencart does not allow you to specify ounces for your products weight when your global settings is for pounds (lbs). This is very frustrating when, for me, most of my products are less than a pound. This throws off weight calculations when the customer checks out and shipping is calculated.

I found this out the hard way when I received an email from a potential customer asking if I could ship the products using DHL instead of the US Postal office because she did not want to spend $150 for shipping. You see, I had put a weight of 1 for most of my products. Dumb idea I know.

The solution was for me to write a quick javascript program that would convert the weight entered to the fraction of a pound.

Here is the code, look at the comments of the script to see the easy installation. Please note that this is changing one of the programs that is apart of Zencart so any future updates may wipe these changes away. You will just have to re-add the additions.

/*
 * jabcustom.js
 * Zencart Product Weight Conversion From Ounces to Pounds
 * http://www.theunixcode.com
 * Version 1.0
 * Free to use or modify at your leisure
 *
 * Requires jquery
 *
 * Installation:
 * Copy this file into your /<admin>/includes directory
 * Add the following to your /<admin>/product.php file just before the </head> tag:
 *     <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 *     <script language="javascript" src="includes/jabcustom.js"></script>
 */

// Gota love jQuery

(function($) {
    $(document).ready(function(){

        // round to 2 decimal spots
        function roundIt(val){
            var dec = 2;
            var newval = 0;
            newval = Math.round(val*Math.pow(10, dec))/ Math.pow(10, dec);
            return newval;
        }

        // Add some documentation
        $('input:text[name=products_weight]').after(' - &nbsp<span>include "oz" to convert from ounces. Example: 15oz</span>')

        // Look for 'oz' in the weight field
        $('input:text[name=products_weight]').change(function() {
            if ($(this).val().match(/oz/i)) {
                var oz = $(this);
                var oval = $(oz).val().replace(/oz/i,'');
                var lb = parseFloat( oval / 16);
                $(oz).attr('value','');
                $(oz).val( roundIt(lb));
            }
        });

    })
})( jQuery );

Leave a Reply