Page 1 of 1

weight.php - devision by zero error (with fix)

Posted: Wed Jul 11, 2012 10:52 am
by DaneSoul
There is bug in file system/library/weight.php
String 38

Code: Select all

return $value * ($to / $from);
cause division by zero if

Code: Select all

weights[$from]
wasn't set befor.

Fix is easy: just add after string 29

Code: Select all

return 0;

Re: weight.php - devision by zero error (with fix)

Posted: Mon Jul 16, 2012 11:53 am
by allenshea
We also have similar problem with this Weight.php

If We use the old version of Weight.php, our shipping extension is no problem. But if changed from

Code: Select all

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

Code: Select all

		if (isset($this->weights[$from])) {
			$from = $this->weights[$from]['value'];
		} else {
			$from = 0;
		}
		
		if (isset($this->weights[$to])) {
			$to = $this->weights[$to]['value'];
		} else {
			$to = 0;
		}	
		
		return $value * ($to / $from);
we got
PHP Warning: Division by zero in ******\PHP\htdocs\online\system\library\weight.php on line 38
If added your code, we got
Warning: No Shipping options are available. Please contact us for assistance!
So, currently we still use the old version of weight.php

Re: weight.php - devision by zero error (with fix)

Posted: Fri Aug 10, 2012 9:54 pm
by toddzy
allenshea wrote:So, currently we still use the old version of weight.php
from which version of opencart do you use the old version of weight.php??? cheers. :)

Re: weight.php - devision by zero error (with fix)

Posted: Fri Aug 10, 2012 9:56 pm
by toddzy
DaneSoul wrote:Fix is easy: just add after string 29

Code: Select all

return 0;
i tried this, but while it avoids the error message, it still fails to realise the cart's weight, which means weight based shipping calculation isn't accurate. ???

Re: weight.php - devision by zero error (with fix)

Posted: Sat Aug 11, 2012 2:25 am
by growlbox
You guys are probably using a second or different language. Go check out if all required field of the lenght and weight classes are filled. See under localisation.

Re: weight.php - devision by zero error (with fix)

Posted: Thu Aug 30, 2012 8:54 pm
by ralphstirrat
I've got this issue too and I've checked all my measurements and I'm not using a 2nd language, this is a new install (154)

Re: weight.php - devision by zero error (with fix)

Posted: Sun Sep 16, 2012 6:32 am
by Klimskady
I too have this error with 1.5.4 with only one language installed, is there a fix for this?

Here is the contents of my weight.php file.

Code: Select all

<?php
class Weight {
	private $weights = array();
	
	public function __construct($registry) {
		$this->db = $registry->get('db');
		$this->config = $registry->get('config');
		
		$weight_class_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
    	
		foreach ($weight_class_query->rows as $result) {
      		$this->weights[$result['weight_class_id']] = array(
        		'weight_class_id' => $result['weight_class_id'],
        		'title'           => $result['title'],
				'unit'            => $result['unit'],
				'value'           => $result['value']
      		); 
    	}
  	}
	  
  	public function convert($value, $from, $to) {
		if ($from == $to) {
      		return $value;
		}
		
		if (isset($this->weights[$from])) {
			$from = $this->weights[$from]['value'];
		} else {
			$from = 0;
		}
		
		if (isset($this->weights[$to])) {
			$to = $this->weights[$to]['value'];
		} else {
			$to = 0;
		}	
		
		return $value * ($to / $from);
  	}

	public function format($value, $weight_class_id, $decimal_point = '.', $thousand_point = ',') {
		if (isset($this->weights[$weight_class_id])) {
    		return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[$weight_class_id]['unit'];
		} else {
			return number_format($value, 2, $decimal_point, $thousand_point);
		}
	}
	
	public function getUnit($weight_class_id) {
		if (isset($this->weights[$weight_class_id])) {
    		return $this->weights[$weight_class_id]['unit'];
		} else {
			return '';
		}
	}	
}
?>

Re: weight.php - devision by zero error (with fix)

Posted: Tue Nov 13, 2012 12:04 am
by ralphstirrat
I finally resolved this problem by making sure all my products had a weight AND a measurement besides them (kg)

works fine now :)

Re: weight.php - devision by zero error (with fix)

Posted: Sun Dec 30, 2012 12:45 am
by growlbox
ralphstirrat wrote:I finally resolved this problem by making sure all my products had a weight AND a measurement besides them (kg)

works fine now :)
This is the solution. Well it also worked for me. Use the Export/Import tool (http://www.opencart.com/index.php?route ... t%20export) to quickly add some values for the weight, length, height, width for all products to fix this issue.

Re: weight.php - devision by zero error (with fix)

Posted: Sun Apr 28, 2013 5:24 am
by Redfox
This bug is simple to fix. My desition is

Code: Select all

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

Re: weight.php - devision by zero error (with fix)

Posted: Sun Apr 28, 2013 5:24 pm
by JFOC
check github commit its been fixed as i remembered

Re: weight.php - devision by zero error (with fix)

Posted: Thu May 23, 2013 7:22 pm
by m3xp2013
I finally resolved this problem:

public function convert($value, $from, $to) {
if ($from == $to) {
return $value;
}

if (isset($this->weights[$from])) {
$from = $this->weights[$from]['value'];
} else {
$from = 0;
}

if (isset($this->weights[$to])) {
$to = $this->weights[$to]['value'];
} else {
$to = 0;
}

if($from == 0)
return $value;
return @($value * ($to / $from));
}

Re: weight.php - devision by zero error (with fix)

Posted: Fri Mar 28, 2014 7:05 pm
by PaulD123
Thank you,

This really saved me! I was soooooo stuck and I just could not get it to work. The above worked perfectly :-)

Thank you again!!!!

Best wishes,

Paul.

Re: weight.php - devision by zero error (with fix)

Posted: Fri Jan 30, 2015 12:52 pm
by IP_CAM
m3xp2013 wrote:I finally resolved this problem:
I just found exactly the same error, when playing around with my latest OC v.1.5.6.4/v.1.5.6.5_rc-Version.
Lucky me, I found this Super-Solution to it! Thanks a lot!

Ernie
bigmax.ch/shop/

Code: Select all

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

Re: weight.php - devision by zero error (with fix)

Posted: Thu May 07, 2015 12:10 pm
by ttpenha
Hey Guys.. I tried everything I read in this post!
I have 2 languages!
English running perfect!
And
Portuguese running with that error
PHP Warning: Division by zero in ******\PHP\htdocs\online\system\library\weight.php on line 38
Anyone halp me?
Thanks
Dhyogo!