[Skip to main content](#vessel-content)

#   Change Price Based on Cost Prive + Margin Fields

##

- **[Geoffc](/forum/user/35341-geoffc "View Geoffc's Profile")**
- [![Geoffc's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/35341-geoffc "View Geoffc's Profile") Topic Author
- [ Offline](/forum/user/35341-geoffc)

Less More

- Posts: 18
- Thank you received: 0
-
-

11 years 3 months ago [\#207081](#207081) by [Geoffc](/forum/user/35341-geoffc "View Geoffc's Profile")  *[Change Price Based on Cost Prive + Margin Fields](/forum/31-development/877919-change-price-based-on-cost-prive-margin-fields#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 &quot;cost price&quot; and a custom field for &quot;margin&quot; 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")* was created by *[Geoffc](/forum/user/35341-geoffc "View Geoffc's Profile")*

-- 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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg3NzkxOS1jaGFuZ2UtcHJpY2UtYmFzZWQtb24tY29zdC1wcml2ZS1tYXJnaW4tZmllbGRzP3RtcGw9bWFya2Rvd24=) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86290
- Thank you received: 14268
-
-

11 years 3 months ago -  11 years 3 months ago [\#207083](#207083) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Change Price Based on Cost Prive + Margin Fields](/forum/31-development/877919-change-price-based-on-cost-prive-margin-fields#207083 "-- 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 &quot;cost price&quot; and a custom field for &quot;margin&quot; 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")*

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](http://take.ms/egFnL)

Please replace the function "updateValuesSecure()" by: Code:

function updateValuesSecure(&amp;$action,&amp;$possibleTables = array(),&amp;$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','&lt;')){ if(preg_match('/joomla_/',$possibleTable)){ $fieldsTable = $db-&gt;getTableFields('#__'.str_replace('joomla_','',$possibleTable)); $fields = reset($fieldsTable); // foreach($fields as $key =&gt; $field){ // $fields['joomla_'.$key] = $fields[$key]; // unset($fields[$key]); // } $fieldsTable = $fields; } else{ $fieldsTable = $db-&gt;getTableFields('#__hikashop_'.$possibleTable); $fields = reset($fieldsTable); } } else { if(preg_match('/joomla_/',$possibleTable)){ $fields = $db-&gt;getTableColumns('#__'.str_replace('joomla_','',$possibleTable)); // foreach($fields as $key =&gt; $field){ // $fields['joomla_'.$key] = $fields[$key]; // unset($fields[$key]); // } } else $fields = $db-&gt;getTableColumns('#__hikashop_'.$possibleTable); } $mainFields = array_merge($mainFields,$fields); } foreach($mainFields as $key =&gt; $field){ $field = str_replace(',','',$field); if($key == $action['type']){ switch($action['operation']){ case 'int': if(in_array($field,array('boolean'))){ $app-&gt;enqueueMessage(JText::sprintf( 'WRONG_COLUMN_TYPE', $field)); $queryTables = ''; } break; case 'float': if(in_array($field,array('int','boolean'))){ $app-&gt;enqueueMessage(JText::sprintf( 'WRONG_COLUMN_TYPE', $field)); $queryTables = ''; } break; case 'string': if(!in_array($field,array('varchar','text','char'))){ $app-&gt;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-&gt;quote($action['value']);} elseif($action['operation'] == 'operation'){ // Regex $symbols = array('%','+','-','/','*','(',')'); $string = str_replace($symbols,'&#124;&#124;',$action['value']); $entry = array(); $entries = explode('&#124;&#124;',$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']) &amp;&amp; $type == 'table'){ if(!in_array($string['table'], $possibleTables)){ $app-&gt;enqueueMessage(JText::sprintf('TABLE_NOT_EXIST',$string['table'])); $queryTables = ''; continue; } if(!in_array($string, $queryTables)){ $queryTables[] = 'hk_'.$string; } $type = 'column'; }elseif(isset($string['column']) &amp;&amp; $type == 'column'){ $colKey = array(); foreach($mainFields as $key =&gt; $field){ $colKey[] = $key; } if(!in_array($string['column'], $colKey)){ $app-&gt;enqueueMessage(JText::sprintf('COLUMN_NOT_EXIST',$string['column'])); $queryTables = ''; } $type = 'table'; } } } // if !number if(!preg_match('/^(?:\d+&#124;\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-&gt;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: 11 years 3 months ago by [Xavier](/forum/user/4105-xavier "View Xavier's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg3NzkxOS1jaGFuZ2UtcHJpY2UtYmFzZWQtb24tY29zdC1wcml2ZS1tYXJnaW4tZmllbGRzP3RtcGw9bWFya2Rvd24=) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop- Install &amp; Update- Product &amp; Category Display- Content Creation- Sales and Orders- - Orders management- - Reports &amp; Statistics- Customers- Checkout- Payment Methods- Shipping Methods- Import &amp; Export (CSV, XLS…)- Specific Features- - Affilation- - Taxes- - Wishlist- - Vote- - Carousel &amp; Slider- - Mass Actions- - Filters &amp; Search- - Discount &amp; Coupon- - Feature Request- Integrations- Showcase- General Talk about HikaShop- Development- - HikaShop 3.0 Beta- Translation- Commercial Jobs HikaShop Components- HikaMarket- - Market: Feature requests- - Market: How to?- - Market: Bug Report- HikaSerial- - General talk about HikaSerial- - Serial: Bug Report- - Serial: How to?- - Support en Français de HikaSerial (French Support)- - HikaSubscription- HikaAuction- - Auction: Feature requests- - Auction: How to?- - Auction: Bug report Hikashop around the world- Support en Français (French Support)- Russian- - FAQ. Курс выживания с Hikashop. Частые вопросы и о- - Расширения Hikashop- - Наши сайты на Hikashop- 日本語版サポート(Japanese Support)- Thai- German Archives- Bug Report- How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.130 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)

[](#top)
