Post by Kakur007 » Tue Jun 16, 2015 7:21 pm

Dear friends,

I have just installed new local(Estonian) shipping modules to store based on OC 1.5.6.4

The problem appears when going to checkout page. Getting following printout "could not locate length class id for "cm".

Here is the shipping module code

Code: Select all

<?php
class ModelShippingsmartpost extends Model {
	var $cost;
	var $cost_m;
	var $cost_l;
	var $cost_xl;
	var $showShort;
	var $applySort;
	var $enableFree;
	var $freeLimit;
	var $perItem;
	function getQuote($address) {
		$this->showShort = $this->config->get('smartpost_showshort');
		$this->applySort = $this->config->get('smartpost_applysort');
		$this->load->language('shipping/smartpost');
		
		if ($this->config->get('smartpost_status')) {
      		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('smartpost_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
		
      		if (!$this->config->get('smartpost_geo_zone_id')) {
        		$status = TRUE;
      		} elseif ($query->num_rows) {
        		$status = TRUE;
      		} else {
        		$status = FALSE;
      		}
		} else {
			$status = FALSE;
		}
		$this->cost = (double)$this->tax->calculate($this->config->get('smartpost_cost'), $this->config->get('smartpost_tax_class_id'), null);
		$this->cost_m = (double)$this->tax->calculate($this->config->get('smartpost_cost_m'), $this->config->get('smartpost_tax_class_id'), null);
		$this->cost_l = (double)$this->tax->calculate($this->config->get('smartpost_cost_l'), $this->config->get('smartpost_tax_class_id'), null);
		$this->cost_xl = (double)$this->tax->calculate($this->config->get('smartpost_cost_xl'), $this->config->get('smartpost_tax_class_id'), null);
		$this->enableFree = ($this->config->get('smartpost_enablefree') == '1')?true:false;
		$this->freeLimit = $this->config->get('smartpost_freelimit');
		$this->perItem = ($this->config->get('smartpost_peritem') == '1')?true:false;
		$cartSubtotal = $this->cart->getSubTotal();
//		print_r($cartSubtotal);
		if ($this->enableFree && $cartSubtotal >= $this->freeLimit) {
			$this->cost = 0;
			$this->cost_m = 0;
			$this->cost_l = 0;
			$this->cost_xl = 0;
		}
		


		$method_data = array();
		$checkbox = 'smartpost.smartpost_select';
//		print_r($this->session->data['shipping_method']);
		if (isset($this->session->data['shipping_method']) && strpos($this->session->data['shipping_method']['id'], 'smartpost') !== false) {
			$checkbox = $this->session->data['shipping_method']['id'];
		}
		$inCartProducts = $this->cart->getProducts();
		$shippingCosts = array();
		$length_class_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "length_class mc LEFT JOIN " . DB_PREFIX . "length_class_description mcd ON (mc.length_class_id = mcd.length_class_id) WHERE mcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		$cm = '';
    	foreach ($length_class_query->rows as $result) {
			if ($result['unit'] == 'cm') {
				$cm = $result['length_class_id'];
				break;
			}
    	}
    	if ($cm == '') {
    		die('could not locate length class id for "cm"');
    	}
		
		
		
		foreach ($inCartProducts as $product) {
			$class = $product['length_class_id'];
			$height = $this->length->convert($product['height'], $class, $cm);
			$width = $this->length->convert($product['width'], $class, $cm);
			$length = $this->length->convert($product['length'], $class, $cm);
			$minSide = min(array($height, $width, $length));
			$maxSide = max(array($height, $width, $length));
			$sizes = array($height, $width, $length);
	    	sort($sizes);
			
			
			if ($maxSide > 60 || $sizes[0] > 36) {
				//too big for smartpost, disabling....
				$status = false;
				break;
			}
			if ($sizes[0] <= 12 && $sizes[1] <= 36) {
				//small
				for ($i = 1; $i <= $product['quantity']; $i++) {
					$shippingCosts[] = $this->cost;
				}
				continue;
			}
			if ($sizes[0] <= 20 && $sizes[1] <= 36) {
				//medium
				for ($i = 1; $i <= $product['quantity']; $i++) {
					$shippingCosts[] = $this->cost_m;
				}
				continue;
			}
			if ($sizes[0] <= 36 && $sizes[1] <= 38) {
				//large
				for ($i = 1; $i <= $product['quantity']; $i++) {
					$shippingCosts[] = $this->cost_l;
				}
				continue;
			}
			//it is the largest pack
			for ($i = 1; $i <= $product['quantity']; $i++) {
				$shippingCosts[] = $this->cost_xl;
			}
			
//			print_r($this->length->convert($product['height'], $class, 'cm'));
		}
	
		if ($status) {
			$shippingTotal = 0;
			if ($this->perItem) {
				foreach ($shippingCosts as $c) {
					$shippingTotal += (double)$c;
				}
			} else {
				$shippingTotal = max($shippingCosts);
			}
			$quote_data = array();
			$quotes = $this->_quote();

			$dropSelect = '';
			$dropSelect .= '</label><select name="kala" id="kala" style="width: 250px;" onmousemove="" onclick="this.parentNode.parentNode.getElementsByTagName(\'input\')[0].checked = true;this.parentNode.parentNode.getElementsByTagName(\'input\')[0].value = document.getElementById(\'kala\').value;" onchange="this.parentNode.parentNode.getElementsByTagName(\'input\')[0].checked = true;this.parentNode.parentNode.getElementsByTagName(\'input\')[0].value = document.getElementById(\'kala\').value;">';
			
			foreach ($quotes as $q) {
				if (isset($this->session->data['shipping_method']) && $this->session->data['shipping_method']['id'] == 'smartpost.smartpost'.$q['id']) {
					$dropSelect .= "<option value='".'smartpost.smartpost'.$q['id']."' selected='selected'>".$q['title']."</option>\r\n";
				} else {
					$dropSelect .= "<option value='".'smartpost.smartpost'.$q['id']."'>".$q['title']."</option>\r\n";
				}
			}
//			print_r($this->cart->getProducts());
			$dropSelect .= "</select>";
			$dropSelect .= "<label> &nbsp;&nbsp;&nbsp; ";
			
			if (!isset($_POST['country_id'])) {
			
				$dropSelect .= '<script type="text/javascript">jQuery(document).ready(function() {var v = document.getElementById(\'kala\').parentNode.parentNode.nextSibling; while(v && v.nodeType != 1) {v = v.nextSibling;} v.style.display = \'none\';document.getElementById(\'kala\').parentNode.parentNode.getElementsByTagName(\'input\')[0].value = document.getElementById(\'kala\').value;});</script>';
				$dropSelect .= '<script type="text/javascript">var iuuuuu = function() {var v = document.getElementById(\'kala\').parentNode.parentNode.nextSibling; while(v && v.nodeType != 1) {v = v.nextSibling;} v.style.display = \'none\';document.getElementById(\'kala\').parentNode.parentNode.getElementsByTagName(\'input\')[0].value = document.getElementById(\'kala\').value;}; iuuuuu();</script>';
			}

      		$quote_data['smartpost.smartpost_select'] = array(
        		'id'           => $checkbox,
        		'code'           => $checkbox,
        		'title'        => $dropSelect,
        		'cost'         => $shippingTotal,
        		'tax_class_id' => $this->config->get('smartpost_tax_class_id'),
				'text'         => $this->currency->format($this->tax->calculate($shippingTotal, $this->config->get('smartpost_tax_class_id'), $this->config->get('config_tax')))
      		);

			if (!isset($_POST['country_id'])) {
			/* begin start section hack */
      		$quote_data['smartpost.smartpost_begin'] = array(
        		'id'           => 'smartpost.smartpost_begin',
        		'code'           => 'smartpost.smartpost_begin',
        		'title'        => '<!--',
        		'cost'         => $shippingTotal,
        		'tax_class_id' => $this->config->get('smartpost_tax_class_id'),
				'text'         => $this->currency->format($this->tax->calculate($shippingTotal, $this->config->get('smartpost_tax_class_id'), $this->config->get('config_tax')))
      		);
			/* end start section hack */
			
			foreach ($quotes as $q) {
	      		$quote_data['smartpost'.$q['id']] = array(
	        		'id'           => 'smartpost.smartpost'.$q['id'],
	        		'code'           => 'smartpost.smartpost'.$q['id'],
	        		'title'        => 'Smartpost - '.$q['title'],
	        		'cost'         => $shippingTotal,
	        		'tax_class_id' => $this->config->get('smartpost_tax_class_id'),
					'text'         => $this->currency->format($this->tax->calculate($shippingTotal, $this->config->get('smartpost_tax_class_id'), $this->config->get('config_tax')))
	      		);
			}
			
			
			/* begin end section hack */
      		$quote_data['smartpost.smartpost_end'] = array(
        		'id'           => 'smartpost.smartpost_end',
        		'code'           => 'smartpost.smartpost_end',
        		'title'        => '-->',
        		'cost'         => $shippingTotal,
        		'tax_class_id' => $this->config->get('smartpost_tax_class_id'),
				'text'         => $this->currency->format($this->tax->calculate($shippingTotal, $this->config->get('smartpost_tax_class_id'), $this->config->get('config_tax')))
      		);
			/* end end section hack */
			}
			

      		$method_data = array(
        		'id'         => 'smartpost',
        		'code'         => 'smartpost',
        		'title'      => $this->language->get('text_title'),
        		'quote'      => $quote_data,
				'sort_order' => $this->config->get('smartpost_sort_order'),
        		'error'      => FALSE
      		);
		}
	
		return $method_data;
	}
    function _quote($city = '', $state = '') {
	    $city = htmlentities($city);
	    $state = htmlentities($state);
		//    echo $filter;
		//    $body = file_get_contents("http://www.smartpost.ee/places.php");
	    $filename = "smpostcache.txt";
	    if (file_exists($filename) && (time() - filemtime($filename)) < 86400 && filesize($filename) > 0) {
			$fhandle = fopen($filename, "r");
      		$body = fread($fhandle, filesize($filename));
      		fclose($fhandle);
			//      $body = file_get_contents("smpostcache.txt");
	    } else {
		    $body = '';
			$fp = fsockopen("www.smartpost.ee", 80, $errno, $errstr, 30);
			if (!$fp) {
				echo "$errstr ($errno)<br />\n";
			} else {
				$out = "GET /places.php HTTP/1.1\r\n";
				$out .= "Host: www.smartpost.ee\r\n";
				$out .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
				$out .= "Connection: Close\r\n\r\n";
				fputs($fp, $out);
				$content = "";
				$header = "not yet";

				while( !feof( $fp ) ) {
					$line = fgets( $fp, 4096 );
	    			if( $line == "\r\n" && $header == "not yet" ) {
	        			$header = "passed";
					}
					if( $header == "passed" ) {
	        			$content .= $line;
	    			}
				}
				$body = $this->unchunk($content);
	      		$response = unserialize($body);
			    fclose($fp);
			}

		    if (is_writable("smpostcache.txt") || is_writable(getcwd())) {
		        $f = @fopen($filename, 'w');
		        if (!$f) {
				//            return false;
		        } else {
	            	$bytes = fwrite($f, $body);
	            	fclose($f);
		        }
		    }
	    
	    }


		$response = unserialize($body);
		$result = array();
		foreach ($response as $r) {
	      	if ($r['active'] == 1) {
		      	if (($city == '' && $state == '') || 
	      		(($city != '' && $state != '') && 
	      			(stripos(htmlentities($r['name'], ENT_COMPAT, "UTF-8"), $city) !== false || stripos(htmlentities($r['group_name'], ENT_COMPAT, "UTF-8"), $state) !== false))) {
	      			if ($this->showShort) {
				      	$result[] = array('id' => $this->code."_".$r['place_id'],
				      	'code' => $this->code."_".$r['place_id'],
	      						'title' => htmlentities($r['name'], ENT_COMPAT, "UTF-8")."",
	      						'group_sort' => $r['group_sort'],
	      						'cost' => $this->cost);
	      			} else {
				      	$result[] = array('id' => $this->code."_".$r['place_id'],
				      	'code' => $this->code."_".$r['place_id'],
	      						'title' => htmlentities($r['name'].", ".$r['description'].", ".$r['opened']." (".$r['address']." ".$r['city'].", ".$r['group_name'].")", ENT_COMPAT, "UTF-8")."",
	      						'group_sort' => $r['group_sort'],
	      						'cost' => $this->cost);
	      			}
	      	}
	     }
      }
		if ($this->applySort) {
			usort($result, array('ModelShippingsmartpost', 'sort'));
		}
      return $result;

    }
	static function sort($a, $b) {
		$a = str_pad(100 - $a['group_sort'], 3, "0", STR_PAD_LEFT).$a['title'];
		$b = str_pad(100 - $b['group_sort'], 3, "0", STR_PAD_LEFT).$b['title'];
	    return strcmp($a, $b);	
    }
    
	function unchunk($str)	{
	    if (!is_string($str) or strlen($str) < 1) { return false; }
	    $eol = "\r\n";
	    $add = strlen($eol);
	    $tmp = $str;
	    $str = '';
	    do {
	        $tmp = ltrim($tmp);
	        $pos = strpos($tmp, $eol);
	        if ($pos === false) { return false; }
	        $len = hexdec(substr($tmp,0,$pos));
	        if (!is_numeric($len) or $len < 0) { return false; }
	        $str .= substr($tmp, ($pos + $add), $len);
	        $tmp  = substr($tmp, ($len + $pos + $add));
	        $check = trim($tmp);
	        } while(!empty($check));
	    unset($tmp);
	    return $str;
    }
    
    function unchunk2($result) {
    return preg_replace_callback(
        '/(?:(?:\r\n|\n)|^)([0-9A-F]+)(?:\r\n|\n){1,2}(.*?)'.
        '((?:\r\n|\n)(?:[0-9A-F]+(?:\r\n|\n))|$)/si',
        create_function(
            '$matches',
            'return hexdec($matches[1]) == strlen($matches[2]) ? $matches[2] : $matches[0];'
        ),
        $result
    );
	}
}
?>
Looking some help to get this fixed. Store is located at www.digitaltrade.eu

Regards,
Kaur

GoGoNano is passionate about creating eco-friendly bio cleaners and waterproofing sprays that people feel safe to use within their home and around their family.
https://www.gogonano.com


New member

Posts

Joined
Sun Nov 17, 2013 8:44 pm


Post by IP_CAM » Tue Jun 16, 2015 11:11 pm

Did you set your Length Class Admin Section correctly ?
Ernie
ipc.li/cart/
Last edited by IP_CAM on Wed Jun 17, 2015 3:10 am, edited 2 times in total.

My Github OC Site: https://github.com/IP-CAM
5'600 + 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 Kakur007 » Wed Jun 17, 2015 2:41 am

Hehe, never thought about it. I had it in 2 languages, English and Estonian and it had no translation. Now it appears fine. Thanks :)

GoGoNano is passionate about creating eco-friendly bio cleaners and waterproofing sprays that people feel safe to use within their home and around their family.
https://www.gogonano.com


New member

Posts

Joined
Sun Nov 17, 2013 8:44 pm

Who is online

Users browsing this forum: No registered users and 96 guests