I am creating a shopping cart in PHP and a particular item is buy one get one half price. When the user purchases the item, I would get like the offer to be deducted from the total but I'm stuck on how I would do this mathematically.
So far I have something like this in a if loop getting data from database:
$total = $total+($arraycart['Price']*$quantity);
Then I think it will be something along the lines of:
if ($arraycart['Item'] == "A1" and $quantity > 1) {
//calculate here buy one get one half price
}
Any help appreciated.
<?php
$total = 0;
$arraycart['Price'] = 10;
$arraycart['Item'] = 'A1';
$quantity = 3; // change item quantity here
if ($arraycart['Item'] == "A1" and $quantity % 2 == 0 ) {
//calculate here buy one get one half price
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half;
} else {
$quantity = $quantity-1;
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half+$arraycart['Price'];
}
echo $total;
?>