Page 1 of 1

[1.4.4] Weight Conversion formula is Flipped

Posted: Tue Mar 23, 2010 7:06 pm
by SuperJuice
Can someone please explain what is going on in the weight conversion function? it seems to be doing the opposite of what I would expect.

The below is taken from a completely standard OpenCart 1.4.4 install and the cart has a single 2kg item in it (so should be calculated as 2000 grams). The install is set to the default weight class of lbs (as I said, unchanged)

I added some debug code to the weight conversion function. When I load the cart, this is what I get.

This is the debug code in case you think it is to blame (just 5 additional echo lines to display the variables)

Code: Select all

        public function convert($value, $from, $to) {
                echo "Entering convert function<br />";
                echo "Input Value: [" . $value ."]<br />";
                echo "From: [" . $from . "]<br />";
                echo "To: [" . $to . "]<br />";

                if ($from == $to) {
                return $value;
                }

                if (!isset($this->weights[strtolower($from)]) || !isset($this->weights[strtolower($to)])) {
                        return $value;
                } else {
                        $from = $this->weights[strtolower($from)]['value'];
                        $to = $this->weights[strtolower($to)]['value'];

                        echo "Returning: [" . $value * ($from / $to) . "]<br /><br />";[/color]
                        return $value * ($from / $to);
                }
        }
This is the output I get when checking out the cart:

Entering convert function
Input Value: [2]
From: [kg]
To: [lb]
Returning: [0.907194048807] <-- 2kg is ~4.4 lbs
But 0.9 Kilograms is approximately 2 lbs.. the from / to values are reversed????

I want to go _from_ my value of 2kg's to my value in lbs..

Entering convert function
Input Value: [0.907194048807]
From: [lb]
To: [g]
Returning: [0.002] <--.. um what? 0.9 lbs is 0.002 grams? am I reading this right?

0.9g = 0.002 lbs.. once again.. the from and to seem reversed?

I want to go _from_ my value of 0.9 pounds, _to_ my value in grams.

Why does this from/to not make sense?

Am I following this function correctly? are these the results I should be expecting? :|

Re: Weight Conversion formula is Flipped

Posted: Tue Mar 23, 2010 7:48 pm
by Daniel
i'll look at this again. maybe i need to change the weight and lenght class back to the old way.

did you set your default weight to 1.0000?

Re: Weight Conversion formula is Flipped

Posted: Tue Mar 23, 2010 7:50 pm
by Qphoria
I can confirm this.
I just tested it with 1.3.4 and 1.4.4 side by side

1.3.4 uses the old method and a 10kg item converts to 22.0462262 pounds - CORRECT
1.4.4 uses the new method and a 10kg item converts to 4.535970 pounds - INCORRECT

It's flipped.

If you change this in the convert function (system/library/weight.php):

Code: Select all

return $value * ($from / $to);
to

Code: Select all

return $value * ($to / $from);
Then you get the correct answer: 22.046 lbs.

Going to move this to its own thread for more exposure

Nice find SJ

Re: Weight Conversion formula is Flipped

Posted: Tue Mar 23, 2010 8:08 pm
by JNeuhoff
Daniel wrote:i'll look at this again. maybe i need to change the weight and lenght class back to the old way.

did you set your default weight to 1.0000?

I think the convert function in weight.php should look like this:

Code: Select all

	public function convert($value, $from, $to) {
		if ($from == $to) {
			return $value;
		}
		
		if (!isset($this->weights[strtolower($from)]) || !isset($this->weights[strtolower($to)])) {
			return $value;
		} else {			
			$from = $this->weights[strtolower($from)]['value'];
			$to = $this->weights[strtolower($to)]['value'];
		
			return $value * ($to / $from);
		}
	}


Re: [1.4.4] Weight Conversion formula is Flipped

Posted: Tue Mar 23, 2010 8:15 pm
by Daniel
ok i have added this to the next version.

just noticed the currency converter is right but not the weight and length.

Re: [1.4.4] Weight Conversion formula is Flipped

Posted: Fri Mar 26, 2010 10:38 pm
by webfeetdesign
Where abouts is the weight.php file? I'm looking at a few and can't find the code to replace with the above code.

Re: [1.4.4] Weight Conversion formula is Flipped

Posted: Fri Mar 26, 2010 10:54 pm
by Qphoria
system/library/weight.php

Re: [1.4.4] Weight Conversion formula is Flipped

Posted: Fri Mar 26, 2010 11:34 pm
by webfeetdesign
OK done that thank you, its working now but i'm a bit confused. in the admin I have the product weight set to 252 grams which shows up in the checkout as 252kg what is wrong here? Also if I put in higher weights in grams such as 1002 grams it keeps defaulting the weight at 999.99

Any ideas?