Im sure the solution is fairly simple, my brain however doesn’t appear to be in a right state of mind today. I have a MySQL table storing a pricing matrix of my products.
ID PRODUCT MINIMUM PRICE
1 1 2 15
2 1 4 12
3 1 6 10
So a quick explanation, for the product with the id 1, if the customer has a minimum quantity of 2 in their shopping basket the price of each item is bought down to £15. If the user then puts another 2 products In their basket the price of each with be decreased to £12.
THE PROBLEM: I need a way of validating a new price tier each time one is added. So if someone wants to add a new tier of:
ID PRODUCT MINIMUM PRICE
4 1 3 16
This shouldn’t be allowed as in this case the price of each item would increase if the user chooses to purchase three items rather than two. So I need to ensure that prices entered are decreasing as the quantity increases based on values already in the database.
$query = 'select id,minimum,price from `cat.tier_price` where product = %d order by minimum asc;';
$query = sprintf($query, (int)$identity);
if(!$return = $db->query($query)){
echo 'MySQL error!';
}else{
$quantity = $_POST['quantity'];
$price = $_POST['quantity'];
if($return->num_row){
while($tier = $return->fetch_object){
}
}
}
If I haven’t made this clear enough please do say. Any help would be greatly appreciated.
This could be done by a simple function:
You then call this function whenever a new tier should be added. The function tries to select exactly one record where minimum is lower than the one being inserted and the price is higher then the one being inserted. If this query succeeds it will return 1 (considered as true) and this means that the new tear might be inserted. Otherwise the FALSE will be returned which means that the new tier is incorrect.
But I think this should also do a trick (and is more simple):
Please, let me know if this also works.