Improvement saving/updating product prices
- jameswadsworth
-
Topic Author
- Offline
Less
More
15 years 8 months ago - 15 years 8 months ago #5145
by jameswadsworth
Improvement saving/updating product prices was created by jameswadsworth
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:
We then rewrote the updatePrices function in the product.php file in /adminstrator/components/com_hikashop/classes/
We welcome your comments and if you think it is useful feel free to use it.
James
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:
Code:
<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/
Code:
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
Last edit: 15 years 8 months ago by jameswadsworth.
Please Log in or Create an account to join the conversation.
15 years 8 months ago - 15 years 8 months ago #5155
by nicolas
Replied by nicolas on topic Re: Improvement saving/updating product prices
Thank you for your feedback.
We will implement that function in next version along with the modification to the view for the id :
We will implement that function in next version along with the modification to the view for the id :
Code:
function updatePrices($element,$status){
$filters=array('price_product_id='.$status);
if(count($element->prices)){
$ids = array();
foreach($element->prices as $price){
if(!empty($price->price_id)) $ids = $price->price_id;
}
if(!empty($ids)){
$filters[]= 'price_id NOT IN ('.implode(',',$ids).')';
}
}
$query = 'DELETE FROM '.hikashop::table('price').' WHERE '.implode(' AND ',$filters);
$this->database->setQuery($query);
$this->database->query();
if(count($element->prices)){
$insert = array();
foreach($element->prices as $price){
if(empty($price->price_value)) continue;
if(empty($price->price_id)) $price->price_id = 'NULL';
$line = '('.(int)$price->price_currency_id.','.$status.','.(int)$price->price_min_quantity.','.(float)$price->price_value.','.$price->price_id;
if(hikashop::level(2)){
if(empty($price->price_access)){
$price->price_access = 'all';
}
$line.=','.$this->database->Quote($price->price_access);
}
$insert[]=$line.')';
}
if(!empty($insert)){
$select = 'price_currency_id,price_product_id,price_min_quantity,price_value,price_id';
if(hikashop::level(2)){
$select.=',price_access';
}
$query = 'REPLACE '.hikashop::table('price').' ('.$select.') VALUES '.implode(',',$insert).';';
$this->database->setQuery($query);
$this->database->query();
}
}
}
Last edit: 15 years 8 months ago by nicolas.
Please Log in or Create an account to join the conversation.
- jameswadsworth
-
Topic Author
- Offline
15 years 8 months ago - 15 years 8 months ago #5164
by jameswadsworth
Replied by jameswadsworth on topic Re: Improvement saving/updating product prices
Thanks for the consideration. I had trouble posting the code correctly for the price.php file. I have attached the modified version of this file.
This attachment is hidden for guests.
This attachment is hidden for guests.
Please log in or register to see it.
Last edit: 15 years 8 months ago by jameswadsworth.
Please Log in or Create an account to join the conversation.
Time to create page: 0.163 seconds