Post by DaneSoul » Wed Jul 11, 2012 10:52 am

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;

Newbie

Posts

Joined
Wed Jul 11, 2012 10:46 am

Post by allenshea » Mon Jul 16, 2012 11:53 am

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

I know nothing about PHP and SQL, but I still try my best to understand it.


Active Member

Posts

Joined
Mon Dec 14, 2009 10:01 pm

Post by toddzy » Fri Aug 10, 2012 9:54 pm

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. :)

Newbie

Posts

Joined
Tue Jul 31, 2012 12:49 pm

Post by toddzy » Fri Aug 10, 2012 9:56 pm

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

Newbie

Posts

Joined
Tue Jul 31, 2012 12:49 pm

Post by growlbox » Sat Aug 11, 2012 2:25 am

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.

New member

Posts

Joined
Sat Jun 11, 2011 6:16 pm

Post by ralphstirrat » Thu Aug 30, 2012 8:54 pm

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)

http://colbrook.co.uk


Active Member

Posts

Joined
Wed Aug 29, 2012 7:53 pm

Post by Klimskady » Sun Sep 16, 2012 6:32 am

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 '';
		}
	}	
}
?>

Active Member

Posts

Joined
Tue Jun 07, 2011 7:57 am

Post by ralphstirrat » Tue Nov 13, 2012 12:04 am

I finally resolved this problem by making sure all my products had a weight AND a measurement besides them (kg)

works fine now :)

http://colbrook.co.uk


Active Member

Posts

Joined
Wed Aug 29, 2012 7:53 pm

Post by growlbox » Sun Dec 30, 2012 12:45 am

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.

New member

Posts

Joined
Sat Jun 11, 2011 6:16 pm

Post by Redfox » Sun Apr 28, 2013 5:24 am

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

Newbie

Posts

Joined
Sun Apr 28, 2013 5:18 am

Post by JFOC » Sun Apr 28, 2013 5:24 pm

check github commit its been fixed as i remembered

New member

Posts

Joined
Tue May 29, 2012 12:48 pm


Post by m3xp2013 » Thu May 23, 2013 7:22 pm

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

Newbie

Posts

Joined
Thu May 23, 2013 6:56 pm

Post by PaulD123 » Fri Mar 28, 2014 7:05 pm

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.

Newbie

Posts

Joined
Tue Jul 05, 2011 11:58 pm

Post by IP_CAM » Fri Jan 30, 2015 12:52 pm

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

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by ttpenha » Thu May 07, 2015 12:10 pm

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!

Newbie

Posts

Joined
Thu May 07, 2015 12:08 pm
Who is online

Users browsing this forum: Google [Bot] and 36 guests