Change Price Based on Cost Prive + Margin Fields

  • Posts: 18
  • Thank you received: 0
9 years 4 months ago #207081

-- HikaShop version -- : Business: 2.4.0
-- Joomla version -- : 3.4.2

Hi,

Is there a way to update the price (before tax) in the back end based on the value of custom fields. For example a custom field for "cost price" and a custom field for "margin" and I want the price before tax to be the cost price + margin when I save the item in the product editor in background. I am not talking about bulk changes or bulk actions, just editing the cost price and profit margin for a product in the backend product editor and having the price change.

This is a fundamental part of a retail business with multiple suppliers who are often changing their cost prices and I can not find a simple way to do it in HikaShop.

Thanks,
Geoff

Please Log in or Create an account to join the conversation.

  • Posts: 82866
  • Thank you received: 13373
  • MODERATOR
9 years 4 months ago #207083

Hi,

You can do that with a mass action.
Mass actions can be used for bulk operations, but they can also be used on specific actions.
So for example, if you have two custom fields with the column names costprice and margin, you could create such mass action:
take.ms/egFnL

Please replace the function "updateValuesSecure()" by:

	function updateValuesSecure(&$action,&$possibleTables = array(),&$queryTables = array()){
		$db = JFactory::getDBO();
		$app = JFactory::getApplication();
		$tableType = explode('_',$action['type']);

		// Special cases
		if(preg_match('/order_product/',$action['type'])) $tableType = array('order_product');
		if($tableType[0] == 'joomla') $queryTables[] = $tableType[0].'_'.$tableType[1];
		else $queryTables[] = $tableType[0];

		// check if the column type match with the action type
		$mainFields = array();
		foreach($possibleTables as $possibleTable){
			if(!is_string($possibleTable)) continue;
			if(version_compare(JVERSION,'3.0','<')){
				if(preg_match('/joomla_/',$possibleTable)){
					$fieldsTable = $db->getTableFields('#__'.str_replace('joomla_','',$possibleTable));
					$fields = reset($fieldsTable);
//					foreach($fields as $key => $field){
//						$fields['joomla_'.$key] = $fields[$key];
//						unset($fields[$key]);
//					}
					$fieldsTable = $fields;
				}
				else{
					$fieldsTable = $db->getTableFields('#__hikashop_'.$possibleTable);
					$fields = reset($fieldsTable);
				}
			} else {
				if(preg_match('/joomla_/',$possibleTable)){
					$fields = $db->getTableColumns('#__'.str_replace('joomla_','',$possibleTable));
//					foreach($fields as $key => $field){
//						$fields['joomla_'.$key] = $fields[$key];
//						unset($fields[$key]);
//					}
				}
				else $fields = $db->getTableColumns('#__hikashop_'.$possibleTable);
			}
			$mainFields = array_merge($mainFields,$fields);
		}

		foreach($mainFields as $key => $field){
			$field = str_replace(',','',$field);
			if($key == $action['type']){
				switch($action['operation']){
					case 'int':
						if(in_array($field,array('boolean'))){
							 $app->enqueueMessage(JText::sprintf( 'WRONG_COLUMN_TYPE', $field));
							 $queryTables = '';
						}
						break;
					case 'float':
						if(in_array($field,array('int','boolean'))){
							 $app->enqueueMessage(JText::sprintf( 'WRONG_COLUMN_TYPE', $field));
							 $queryTables = '';
						}
						break;
					case 'string':
						if(!in_array($field,array('varchar','text','char'))){
							 $app->enqueueMessage(JText::sprintf( 'WRONG_COLUMN_TYPE', $field));
							 $queryTables = '';
						}
						break;
				}
			}
		}
		if($action['operation'] == 'int'){$value = (int)$action['value'];}
		elseif($action['operation'] == 'float'){$value = (float)$action['value'];}
		elseif($action['operation'] == 'string'){$value = $db->quote($action['value']);}
		elseif($action['operation'] == 'operation'){
			// Regex
			$symbols = array('%','+','-','/','*','(',')');
			$string = str_replace($symbols,'||',$action['value']);

			$entry = array();
			$entries = explode('||',$string);
			foreach($entries as $entry){
				$data = explode('.',$entry);
				if(!in_array($data[0],$possibleTables))
					continue;
				$strings[]['table'] = $data[0];
				$strings[]['column'] = $data[1];
			}
			$type = 'table';
			if(!empty($mainFields)){
				foreach($strings as $string){
					if(isset($string['table']) && $type == 'table'){
						if(!in_array($string['table'], $possibleTables)){
							$app->enqueueMessage(JText::sprintf('TABLE_NOT_EXIST',$string['table']));
							$queryTables = '';
							continue;
						}
						if(!in_array($string, $queryTables)){
							$queryTables[] = 'hk_'.$string;
						}
						$type = 'column';
					}elseif(isset($string['column']) && $type == 'column'){
						$colKey = array();
						foreach($mainFields as $key => $field){
							$colKey[] = $key;
						}
						if(!in_array($string['column'], $colKey)){
							$app->enqueueMessage(JText::sprintf('COLUMN_NOT_EXIST',$string['column']));
							$queryTables = '';
						}
						$type = 'table';
					}
				}
			}

			// if !number
			if(!preg_match('/^(?:\d+|\d*\.\d+)$/',$action['value'])){
				// if operation (ex: +5, -2) increment the value in the type selected table

				if(in_array($action['value'][0], array('+','-'))){
					$value = $action['type'].$action['value'];
				}
				// else add the prefix
				else{
					$value = $action['value'];
					// Group the available tables in only one entry
					$tables = array();
					foreach($strings as $string){
						if(isset($string['table'])){
							$tables[$string['table']] = $string['table'];
						}
					}
					// add the prefix
					foreach($tables as $table){
						$value = str_replace($table.'.','hk_'.$table.'.',$value);
					}
				}
				$value = strip_tags($value);
			}
			// default value
			else{
				$value = $db->quote($action['value']);
			}
		}else{$value = '';}
		return $value;
	}
in "administrator/components/com_hikashop/classes/massaction.php"

Then, the calculation would be done automatically for you each time you save a product.

Last edit: 9 years 4 months ago by Xavier.

Please Log in or Create an account to join the conversation.

Time to create page: 0.056 seconds
Powered by Kunena Forum