Measure Conversion Error (affects USPS, UPS, etc shipping modules)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
This post will help answer a problem users are seeing with improper dimension and weight conversions in the various shipping modules.  I've also posted this (see text below) on the codeplex (http://nopcommerce.codeplex.com/workitem/10099).  If you are affected please vote it up to get the team's attention for the next release.  Also you can make the change listed to the ConvertToPrimaryMeasureWeight and ConvertToPrimaryMeasureDimension functions.

Happy hunting.


DESCRIPTION POSTED ON CODEPLEX
===========================
While digging through a UPS weight conversion problem I found that the issue wasn't with USPS module but in the ConvertWeight, ConvertDimension functions in MeasureService.cs.  Well actually the problem is in the ConvertToPrimaryMeasureWeight and ConvertToPrimaryMeasureDimension functions.  To successfully convert from one unit of measurement to another we must 1st divide the value by the ratio for the current default unit of measure.  Then multiply that result times the ratio for the target measurement.  

i.e. to go from 8kg to ounces would be 8 / 0.4536 (kg ratio) * 16 (ounces ratio).

The ConvertToPrimaryMeasureWeight and ConvertToPrimaryMeasureDimension functions skips the ability to get the ratio for the current unit of measure.

The line that reads "if (result != decimal.Zero && sourceMeasureWeight.MeasureWeightId != BaseWeightIn.MeasureWeightId)" in the function copy below should be "if (result != decimal.Zero)".  Without it the " / 0.4536 (kg ratio)" in the example above never executes preventing a proper conversion.  I've coded around this in the USPS module I'm working on but thought it would be good for those smarter than me to be aware of.


        public decimal ConvertToPrimaryMeasureWeight(decimal quantity, MeasureWeight sourceMeasureWeight)
        {
            decimal result = quantity;
            if (result != decimal.Zero && sourceMeasureWeight.MeasureWeightId != BaseWeightIn.MeasureWeightId)
            {
                decimal exchangeRatio = sourceMeasureWeight.Ratio;
                if (exchangeRatio == decimal.Zero)
                    throw new NopException(string.Format("Exchange ratio not set for weight [{0}]", sourceMeasureWeight.Name));
                result = result / exchangeRatio;
            }
            return result;
        }
12 years ago
Please see my reply here
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.