How to change standard default product units?

Hello All,

Currently, all new products default to ‘M’ as shown in the picture. We mainly use ‘CM’ for length, but I can’t seem to find where to change the default setting.

units.png

I looked into: \product\class\html.formproduct.class.php

I am guessing this line is responsible:

else if ($measuring_style == 'size') $measuring_units=array(-3=>1,-2=>1,-1=>1,0=>1,98=>1,99=>1);

If I change 0=>1 to 0=>-2 nothing happens on the new product page or the edit product page.

Can anyone help?

anyone?

Have you tried changing the default ‘0’ on line 302?

[code]
function load_measuring_units($name=‘measuring_units’, $measuring_style=’’, $default=‘0’, $adddefault=0)[/code

1 Like

Hello

I tried that and it didn’t work either.

You were very close aljawaid. I’ve managed to set the default values (I use G and MM). I’ve edited two lines (changed 0 to -3) as per below

function load_measuring_units($name='measuring_units', $measuring_style='', $default='-3', $adddefault=0)

function select_measuring_units($name='measuring_units', $measuring_style='', $default='-3', $adddefault=0)
and it worked for me. I hope it’s going to work for you as well. Good luck

1 Like

That worked for my ‘CM’ default values, but it changed my weight to ‘MG’ instead of 'KG. Also my volume won’t change to ‘LT’

ok, then I think that $default='' will need to be broken down into something like $weight_default=''$length_default=''$width_default='' etc. but I’ll need to do a research or tests to give you the right solution

try this:

function load_measuring_units($name='measuring_units', $measuring_style='size', $default='-3', $adddefault=0)

[code]
function select_measuring_units($name=‘measuring_units’, $measuring_style=‘size’, $default=’-3’, $adddefault=0)[/code

that will change just the Length x Width x Height to MM in this case (sorry my mistake) but basically in $measuring_style='' you need to put ‘weight’ ‘size’ ‘surface’ or ‘volume’ it depends on what you want to edit. I’ve tested it quickly just on weight and the weight was changing according to the chosen value (-6,-3,0,etc.) and my Length x Width x Height stayed on MM. So to set the default values for all the fields separately you will probably need to copy or the whole line or just $measuring_style='size', $default='-3' within the brackets. Have a go and let us all know what you came up with

I tried this and whilst it baffled me, none of the options work. I am sure I haven’t done it correctly.

Could you kindly provide the full code section you have changed so I can compare it to mine?

function select_measuring_units($name='measuring_units', $measuring_style='', $default='-2', $adddefault=0)
	{
		print $this->load_measuring_units($name, $measuring_style, $default, $adddefault);
	}

	/**
	 *  Return a combo box with list of units
	 *  For the moment, units labels are defined in measuring_units_string
	 *
	 *  @param	string		$name                Name of HTML field
	 *  @param  string		$measuring_style     Unit to show: weight, size, surface, volume
	 *  @param  string		$default             Force unit
	 * 	@param	int			$adddefault			Add empty unit called "Default"
	 * 	@return	string
	 */
	function load_measuring_units($name='measuring_units', $measuring_style='', $default='0', $adddefault=0)
	{
		global $langs,$conf,$mysoc;
		$langs->load("other");

		$return='';

		$measuring_units=array();
		if ($measuring_style == 'weight') $measuring_units=array(-6=>1,-3=>1,0=>1,3=>1,99=>1);
		else if ($measuring_style == 'size') $measuring_units=array(-3=>1,-2=>1,-1=>1,0=>1,98=>1,99=>1);
        else if ($measuring_style == 'surface') $measuring_units=array(-6=>1,-4=>1,-2=>1,0=>1,98=>1,99=>1);
		else if ($measuring_style == 'volume') $measuring_units=array(-9=>1,-6=>1,-3=>1,0=>1,88=>1,89=>1,97=>1,99=>1,/* 98=>1 */);  // Liter is not used as already available with dm3

		$return.= '<select class="flat" name="'.$name.'">';
		if ($adddefault) $return.= '<option value="0">'.$langs->trans("Default").'</option>';

		foreach ($measuring_units as $key => $value)
		{
			$return.= '<option value="'.$key.'"';
			if ($key == $default)
			{
				$return.= ' selected';
			}
			//$return.= '>'.$value.'</option>';
			$return.= '>'.measuring_units_string($key,$measuring_style).'</option>';
		}
		$return.= '</select>';

		return $return;
	}

I was playing with the code but I haven’t managed to set it up as you wanted (KG and CM) but I’ve used a default value for CM (-2) what in my case is a 50% win as I use now KG and CM. I hope someone will find the way to set it up separately.

[code]

<?php /* Copyright (C) 2008-2009 Laurent Destailleur * Copyright (C) 2016 Francis Appels * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/product/class/html.formproduct.class.php * \brief Fichier de la classe des fonctions predefinie de composants html */ require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; /** * Class with static methods for building HTML components related to products * Only components common to products and services must be here. */ class FormProduct { var $db; var $error; // Cache arrays var $cache_warehouses=array(); /** * Constructor * * @param DoliDB $db Database handler */ function __construct($db) { $this->db = $db; return 1; } /** * Load in cache array list of warehouses * If fk_product is not 0, we do not use cache * * @param int $fk_product Add quantity of stock in label for product with id fk_product. Nothing if 0. * @param string $batch Add quantity of batch stock in label for product with batch name batch, batch name precedes batch_id. Nothing if ''. * @param string $status warehouse status filter, following comma separated filter options can be used * 'warehouseopen' = select products from open warehouses, * 'warehouseclosed' = select products from closed warehouses, * 'warehouseinternal' = select products from warehouses for internal correct/transfer only * @param boolean $sumStock sum total stock of a warehouse, default true * @param array $exclude warehouses ids to exclude * @return int Nb of loaded lines, 0 if already loaded, <0 if KO */ function loadWarehouses($fk_product=0, $batch = '', $status='', $sumStock = true, $exclude='') { global $conf, $langs; if (empty($fk_product) && count($this->cache_warehouses)) return 0; // Cache already loaded and we do not want a list with information specific to a product if (is_array($exclude)) $excludeGroups = implode("','",$exclude); $warehouseStatus = array(); if (preg_match('/warehouseclosed/', $status)) { $warehouseStatus[] = Entrepot::STATUS_CLOSED; } if (preg_match('/warehouseopen/', $status)) { $warehouseStatus[] = Entrepot::STATUS_OPEN_ALL; } if (preg_match('/warehouseinternal/', $status)) { $warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL; } $sql = "SELECT e.rowid, e.label, e.description, e.fk_parent"; if (!empty($fk_product)) { if (!empty($batch)) { $sql.= ", pb.qty as stock"; } else { $sql.= ", ps.reel as stock"; } } else if ($sumStock) { $sql.= ", sum(ps.reel) as stock"; } $sql.= " FROM ".MAIN_DB_PREFIX."entrepot as e"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps on ps.fk_entrepot = e.rowid"; if (!empty($fk_product)) { $sql.= " AND ps.fk_product = '".$fk_product."'"; if (!empty($batch)) { $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_batch as pb on pb.fk_product_stock = ps.rowid AND pb.batch = '".$batch."'"; } } $sql.= " WHERE e.entity IN (".getEntity('stock').")"; if (count($warehouseStatus)) { $sql.= " AND e.statut IN (".$this->db->escape(implode(',',$warehouseStatus)).")"; } else { $sql.= " AND e.statut = 1"; } if(!empty($exclude)) $sql.= ' AND e.rowid NOT IN('.$this->db->escape(implode(',', $exclude)).')'; if ($sumStock && empty($fk_product)) $sql.= " GROUP BY e.rowid, e.label, e.description, e.fk_parent"; $sql.= " ORDER BY e.label"; dol_syslog(get_class($this).'::loadWarehouses', LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $obj = $this->db->fetch_object($resql); if ($sumStock) $obj->stock = price2num($obj->stock,5); $this->cache_warehouses[$obj->rowid]['id'] =$obj->rowid; $this->cache_warehouses[$obj->rowid]['label']=$obj->label; $this->cache_warehouses[$obj->rowid]['parent_id']=$obj->fk_parent; $this->cache_warehouses[$obj->rowid]['description'] = $obj->description; $this->cache_warehouses[$obj->rowid]['stock'] = $obj->stock; $i++; } // Full label init foreach($this->cache_warehouses as $obj_rowid=>$tab) { $this->cache_warehouses[$obj_rowid]['full_label'] = $this->get_parent_path($tab); } return $num; } else { dol_print_error($this->db); return -1; } } /** * Return full path to current warehouse in $tab (recursive function) * * @param array $tab warehouse data in $this->cache_warehouses line * @param String $final_label full label with all parents, separated by ' >> ' (completed on each call) * @return String full label with all parents, separated by ' >> ' */ private function get_parent_path($tab, $final_label='') { if(empty($final_label)) $final_label = $tab['label']; if(empty($tab['parent_id'])) return $final_label; else { if(!empty($this->cache_warehouses[$tab['parent_id']])) { $final_label = $this->cache_warehouses[$tab['parent_id']]['label'].' >> '.$final_label; return $this->get_parent_path($this->cache_warehouses[$tab['parent_id']], $final_label); } } return $final_label; } /** * Return list of warehouses * * @param int $selected Id of preselected warehouse ('' for no value, 'ifone'=select value if one value otherwise no value) * @param string $htmlname Name of html select html * @param string $filterstatus warehouse status filter, following comma separated filter options can be used * 'warehouseopen' = select products from open warehouses, * 'warehouseclosed' = select products from closed warehouses, * 'warehouseinternal' = select products from warehouses for internal correct/transfer only * @param int $empty 1=Can be empty, 0 if not * @param int $disabled 1=Select is disabled * @param int $fk_product Add quantity of stock in label for product with id fk_product. Nothing if 0. * @param string $empty_label Empty label if needed (only if $empty=1) * @param int $showstock 1=Show stock count * @param int $forcecombo 1=Force combo iso ajax select2 * @param array $events Events to add to select2 * @param string $morecss Add more css classes to HTML select * @param array $exclude Warehouses ids to exclude * @param int $showfullpath 1=Show full path of name (parent ref into label), 0=Show only ref of current warehouse * @return string HTML select */ function selectWarehouses($selected='',$htmlname='idwarehouse',$filterstatus='',$empty=0,$disabled=0,$fk_product=0,$empty_label='', $showstock=0, $forcecombo=0, $events=array(), $morecss='minwidth200', $exclude='', $showfullpath=1) { global $conf,$langs,$user; dol_syslog(get_class($this)."::selectWarehouses $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $empty_label, $showstock, $forcecombo, $morecss",LOG_DEBUG); $out=''; if (empty($conf->global->ENTREPOT_EXTRA_STATUS)) $filterstatus = ''; $this->loadWarehouses($fk_product, '', $filterstatus, true, $exclude); $nbofwarehouses=count($this->cache_warehouses); if ($conf->use_javascript_ajax && ! $forcecombo) { include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; $comboenhancement = ajax_combobox($htmlname, $events); $out.= $comboenhancement; $nodatarole=($comboenhancement?' data-role="none"':''); } $out.=''; if ($empty) $out.=''.($empty_label?$empty_label:' ').''; foreach($this->cache_warehouses as $id => $arraytypes) { $out.=' 0)) && ($arraytypes['stock'] != 0 || ($showstock > 0))) $out.=' ('.$langs->trans("Stock").':'.$arraytypes['stock'].')'; $out.=''; } $out.=''; if ($disabled) $out.=''; return $out; } /** * Display form to select warehouse * * @param string $page Page * @param int $selected Id of warehouse * @param string $htmlname Name of select html field * @param int $addempty 1=Add an empty value in list, 2=Add an empty value in list only if there is more than 2 entries. * @return void */ function formSelectWarehouses($page, $selected='', $htmlname='warehouse_id', $addempty=0) { global $langs; if ($htmlname != "none") { print ''; print ''; print ''; print ''; print ''; print ''; print '
'; print $this->selectWarehouses($selected, $htmlname, '', $addempty); print '
'; } else { if ($selected) { require_once DOL_DOCUMENT_ROOT .'/product/stock/class/entrepot.class.php'; $warehousestatic=new Entrepot($this->db); $warehousestatic->fetch($selected); print $warehousestatic->getNomUrl(); } else { print " "; } } } /** * Output a combo box with list of units * pour l'instant on ne definit pas les unites dans la base * * @param string $name Name of HTML field * @param string $measuring_style Unit to show: weight, size, surface, volume * @param string $default Force unit * @param int $adddefault Add empty unit called "Default" * @return void */ function select_measuring_units($name='measuring_units', $measuring_style='size', $default='-2', $adddefault=0) { print $this->load_measuring_units($name, $measuring_style, $default, $adddefault); } /** * Return a combo box with list of units * For the moment, units labels are defined in measuring_units_string * * @param string $name Name of HTML field * @param string $measuring_style Unit to show: weight, size, surface, volume * @param string $default Force unit * @param int $adddefault Add empty unit called "Default" * @return string */ function load_measuring_units($name='measuring_units', $measuring_style='size', $default='-2', $adddefault=0) { global $langs,$conf,$mysoc; $langs->load("other"); $return=''; $measuring_units=array(); if ($measuring_style == 'weight') $measuring_units=array(-6=>1,-3=>1,0=>1,3=>1,98=>1,99=>1); else if ($measuring_style == 'size') $measuring_units=array(-3=>1,-2=>1,-1=>1,0=>1,98=>1,99=>1); else if ($measuring_style == 'surface') $measuring_units=array(-6=>1,-4=>1,-2=>1,0=>1,98=>1,99=>1); else if ($measuring_style == 'volume') $measuring_units=array(-9=>1,-6=>1,-3=>1,0=>1,88=>1,89=>1,97=>1,99=>1,/* 98=>1 */); // Liter is not used as already available with dm3 $return.= ''; if ($adddefault) $return.= ''.$langs->trans("Default").''; foreach ($measuring_units as $key => $value) { $return.= ''.$value.''; $return.= '>'.measuring_units_string($key,$measuring_style).''; } $return.= ''; return $return; } }[/code]

Solution for today’s day is:
In the Home->Setup->Other setup add MAIN_WEIGHT_DEFAULT_UNIT and set it to 0 (0=kg, -3=g, …) then in \product\class\html.formproduct.class.php edit two lines

function load_measuring_units($name='measuring_units', $measuring_style='', $default='0', $adddefault=0)

function select_measuring_units($name='measuring_units', $measuring_style='', $default='0', $adddefault=0)
and change them to

function load_measuring_units($name='measuring_units', $measuring_style='', $default='-2', $adddefault=0)

function select_measuring_units($name='measuring_units', $measuring_style='', $default='-2', $adddefault=0)
The outcome is that your default values are set to kilograms and centimetres but please note that Area will be set to dm2 and Volume to mm3