Bug - USPS for 4.20 - "Please enter a valid weight in ounces"

Posted: August 31, 2019 at 3:09 PM Quote #245526
1. nopCommerce version: 4.20
2. Expected behavior:  USPS should return services/rates
3. Actual behavior  :  USPS reports error "Please enter a valid weight in ounces"
4. Steps to reproduce the problem:
Create a product with a weight of 1.75 lbs  (includes fraction)

The problem is that in the prior versions of the USPS plugin, the var weight was an int.  Now, it's a decimal returned by function GetWeight()

Here's some LinqPad to see the issue

decimal weight = 28.0M;  //weight in ounces

(weight / 16).Dump("(weight / 16)");

//old - incorrect
var pounds = Convert.ToInt32(weight / 16);

//new correct
//var pounds = Convert.ToInt32(Math.Floor(weight / 16));

var ounces = Convert.ToInt32(weight - (pounds * 16.0M));

pounds.Dump("pounds");
ounces.Dump("ounces");



(weight / 16)
1.75

--old--
pounds
2

ounces
-4

--new--
pounds
1

ounces
12


5. Modifications
\Plugins\Nop.Plugin.Shipping.USPS\Services\USPSService.cs

Option 1
Existing code:
private string CreateRequest(...
...
  var weight = GetWeight(getShippingOptionRequest);
  ...
  var pounds = Convert.ToInt32(weight / 16);
  var ounces = Convert.ToInt32(weight - (pounds * 16.0M));
  ...


The correct expression is to use Math.Floor:

  var pounds = Convert.ToInt32(Math.Floor(weight / 16));



Option 2 - better :)
Alternately, leave the above as-is, and change the function GetWeight() to return an int, ...
considering that it's returning weight in ounces as per
   USPSShippingDefaults.MEASURE_WEIGHT_SYSTEM_KEYWORD = "ounce"


Old code
private decimal GetWeight(GetShippingOptionRequest shippingOptionRequest, int minRate = 1)
{
  var measureWeight = _measureService.GetMeasureWeightBySystemKeyword(USPSShippingDefaults.MEASURE_WEIGHT_SYSTEM_KEYWORD)
    ?? throw new NopException($"USPS shipping service. Could not load \"{USPSShippingDefaults.MEASURE_WEIGHT_SYSTEM_KEYWORD}\" measure weight");

  var weight = _shippingService.GetTotalWeight(shippingOptionRequest, ignoreFreeShippedItems: true);
  weight = _measureService.ConvertFromPrimaryMeasureWeight(weight, measureWeight);
  weight = Convert.ToInt32(Math.Ceiling(weight));
  return Math.Max(weight, minRate);
}


New Code
1) return type changed to int
2) parameter
..., int minRate = 1)
renamed to
..., int minWeight = 1)
3) moved the Math.Max & Convert.ToInt32

private int GetWeight(GetShippingOptionRequest shippingOptionRequest, int minWeight = 1)
{
  var measureWeight = _measureService.GetMeasureWeightBySystemKeyword(USPSShippingDefaults.MEASURE_WEIGHT_SYSTEM_KEYWORD)
    ?? throw new NopException($"USPS shipping service. Could not load \"{USPSShippingDefaults.MEASURE_WEIGHT_SYSTEM_KEYWORD}\" measure weight");

  var weight = _shippingService.GetTotalWeight(shippingOptionRequest, ignoreFreeShippedItems: true);
  weight = _measureService.ConvertFromPrimaryMeasureWeight(weight, measureWeight);
  weight = Math.Max(Math.Ceiling(weight), minWeight);
  return Convert.ToInt32(weight);

}



(P.S. There are warnings in the same code file:
Message  IDE0051  Private member 'USPSService.GetDimensionsForSingleItem' is unused.
Message  IDE0051  Private member 'USPSService.GetWeightForSingleItem' is unused.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
www.noptools.com
Posted: September 02, 2019 at 11:00 AM Quote #245581
Thanks for your investigation. Here is a work item for this issue.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Interested in the dedicated Premium support services provided by core developers? Please visit http://www.nopcommerce.com/supportservices.aspx

Regards,
Maksim Romanov
Posted: September 13, 2019 at 7:27 AM Quote #246287
Done. Please see this commit.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Interested in the dedicated Premium support services provided by core developers? Please visit http://www.nopcommerce.com/supportservices.aspx

Regards,
Dmitriy Kulagin
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.