Post by SuperJuice » Tue Mar 23, 2010 7:06 pm

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? :|

Active Member

Posts

Joined
Thu Aug 13, 2009 12:06 pm

Post by Daniel » Tue Mar 23, 2010 7:48 pm

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?

OpenCart®
Project Owner & Developer.


User avatar
Administrator

Posts

Joined
Fri Nov 03, 2006 6:57 pm

Post by Qphoria » Tue Mar 23, 2010 7:50 pm

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by JNeuhoff » Tue Mar 23, 2010 8:08 pm

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);
		}
	}


Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Daniel » Tue Mar 23, 2010 8:15 pm

ok i have added this to the next version.

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

OpenCart®
Project Owner & Developer.


User avatar
Administrator

Posts

Joined
Fri Nov 03, 2006 6:57 pm

Post by webfeetdesign » Fri Mar 26, 2010 10:38 pm

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.

Active Member

Posts

Joined
Fri Feb 05, 2010 1:28 am
Location - Dubai

Post by Qphoria » Fri Mar 26, 2010 10:54 pm

system/library/weight.php

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by webfeetdesign » Fri Mar 26, 2010 11:34 pm

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?

Active Member

Posts

Joined
Fri Feb 05, 2010 1:28 am
Location - Dubai
Who is online

Users browsing this forum: No registered users and 15 guests