Hello everyone,
We have been testing hikashop and we love it. However we believe the saving of product prices can be improved. At the moment if a product is applied/saved, regardless if the prices have been changed or not, all the prices are deleted from the database then re-inserted. It works, but some products might have up to 10 steps in their prices, so this means 10 prices rows must be deleted from the database and then other 10 prices rows inserted again. And this happens every time a product is applied/saved. We have been testing with a few hundred products and already the ID number for prices is going stratospheric. We propose that besides 'deleted' and 'insert' we add an 'update' call using CASE, WHEN, THEN operators in mysql. The 'delete' and 'insert' are only called if there really is something to delete or insert. The 'update' function means the database compares the passed information with currently stored information and only makes a change if there is difference and we reduce the number of calls being made the database significantly. How do we get it work?
First we had to make a small change to the price.php file in /adminstrator/components/com_hikashop/views/product/tmpl/
We want to pass the $row->price_id variable as hidden value so it becomes:
<td>
<?php echo (!empty($row->price_id) ? $row->price_id : '--' ); ?><input type="hidden" id="price[price_id][<?php echo $i;?>]" name="price[price_id][<?php echo $i;?>]" value="<?php echo @$row->price_id; ?>" />
</td>
We then rewrote the updatePrices function in the product.php file in /adminstrator/components/com_hikashop/classes/
function updatePrices($element,$status){
if(count($element->prices)){
$delete_where = array();
$update_price_currency_id = array();
$update_price_value = array();
$update_price_min_quanity = array();
$update_where = array();
$insert = array();
foreach($element->prices as $price){
if(empty($price->price_value)){
$delete_where[] = (int)$price->price_id;
} elseif($price->price_id){
$update_price_currency_id[] = sprintf("WHEN %d THEN %d ", $price->price_id, (int)$price->price_currency_id);
$update_price_value[] = sprintf("WHEN %d THEN '%s' ", $price->price_id, (float)$price->price_value);
$update_price_min_quanity[] = sprintf("WHEN %d THEN %d ", $price->price_id, (int)$price->price_min_quantity);
$update_where[] = (int)$price->price_id;
} else {
$insert[]='('.(int)$price->price_currency_id.','.$status.','.(int)$price->price_min_quantity.','.(float)$price->price_value.')';
}
}
if(!empty($update_where)){
$query = 'UPDATE IGNORE '.hikashop::table('price').' SET price_currency_id = CASE price_id '.implode(' ',$update_price_currency_id).' END, price_value = CASE price_id '.implode(' ',$update_price_value).' END, price_min_quantity = CASE price_id '.implode(' ',$update_price_min_quanity).' END WHERE price_product_id='.$status.' AND price_id IN ('.implode(',',$update_where).');';
$this->database->setQuery($query);
$this->database->query();
}
if(!empty($insert)){
$query = 'INSERT IGNORE INTO '.hikashop::table('price').' (price_currency_id,price_product_id,price_min_quantity,price_value) VALUES '.implode(',',$insert).';';
$this->database->setQuery($query);
$this->database->query();
}
if(!empty($delete_where)){
$query = 'DELETE FROM '.hikashop::table('price').' WHERE price_product_id='.$status.' AND price_id IN ('.implode(',',$delete_where).');';
$this->database->setQuery($query);
$this->database->query();
}
}
}
We welcome your comments and if you think it is useful feel free to use it.
James