I wonder if someone could please help me out a little. I have a loop that loops through the contents of a shopping cart. I wish to apply a 25% discount to additional items purchased. So basically 1st item is full price and every other item is reduced by 25%. I've tried various methods but all i seem to get is the discount apply to all or nothing.
The loop below works perfectly if i remove the if statement and its contents thus not wishing to apply a discount. As it currently stands it does not add a discount at all. If i remove the if condition and use it's contents then it will apply a 25% discount to all items.
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
$Total += $ShoppingCart[$Loop][Price];
if($Loop > 1) {
$Total += $ShoppingCart[$Loop][Price];
$PercentageAmount = 25;
$TotalPrice = $TotalPrice * ((100-$PercentageAmount) / 100);
}
}
Edited:
Unfortunately none of the answers, although maybe technically good, do not fix my problem. I have had to result to placing 2 if statements within a loop and then calculating their combined total. Not an ideal solution but works perfectly never the less. Somehow i need the sort it so then the most expensive item is at full price. It would be much easier if i was not tied to using a loop in this fashion and instead could use array functions.
$i = 0;
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
if($i == 0) {
$Total += $ShoppingCart[$Loop][Price];
}
if($i > 0) {
$TotalMulti += $ShoppingCart[$Loop][Price];
$TotalMulti = $TotalMulti * .75;
}
$i++;
}
$NewTotal = $Total + $TotalMulti;
Here's how I would do it:
$prices = array_column($ShoppingCart, 'Price');
array_walk($prices, function(&$price, $i) { if($i) $price *= .75; });
$total = array_sum($prices);
How it works:
The prices are pulled out in their own array -- I much prefer this because the discount code does not mess with the "normal" prices, which might cause unexpected complications.
The array of prices is iterated over, and every element but the first is set to 75% of its value.
The total price is just the sum of the discounted prices.
This code depends on array_column, which is only available starting with PHP 5.5. For earlier versions you can either grab an implementation from here or substitute this:
$prices = array_map(function($el) { return $el['Price']; }, $ShoppingCart);
If the discount percentage is a variable you will also need this modification:
$discount = .25;
array_walk(
$prices,
function(&$price, $i) use($discount) { if($i) $price *= (1 - $discount); }
);
for($Loop = 0; $Loop < count($Cart); $Loop++){
if($Loop > 0) {
$Total += ($Cart[$Loop]['Price']*0.75);
} else {
$Total += $Cart[$Loop]['Price'];
}
}
Hope this helps:
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
# if not first item then discount price
if($Loop > 1) {
$PercentageAmount = 25;
# grab the price
$itemPrice = $ShoppingCart[$Loop][Price];
# what should the user pay?
$itemDiscountPrice = $itemPrice * (100.0 - $PercentageAmount) / 100.0
# add it to total
$Total += $itemDiscountPrice;
} else {
# if first item -> full price
$Total += $ShoppingCart[$Loop][Price];
}
}
Related
I'm setting up a webshop to use in a printshop but I'm stuck on how to automatically provide prices for different amounts.
In example:
The client wants to see the price for 100 sheets and immediately it should see the price for 150, 200, 250, ... (it's cheaper per piece every time you the amount is larger).
This is easy of course; get $amount and add +50 every time. But when the client asks 1000 it should not be +50 but maybe +100.
Is there a slick and easy way to do this or is it going to be a lot of
if($amount < 100){ add +50 }elseif($amount > 100 && < 500){ add + 75 }....?
Ok, how about this:
$amount = 100; // client's amount
$i = 1; // iterator
$suggested = array(); // array for suggested amounts
$increment = ($amount / 2); // half of client's suggested amount
# while loop
while ($i <= 3) {
$amount = $amount + $increment; // formula
array_push($suggested, $amount); // add to array
$i++;
}
# will output 150, 200, 250
foreach ($suggested as $s) {
echo $s . '<br />';
}
Will that get you started in the right direction?
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;
?>
The user requests a Product Category and says what quantity they want of it, ie Sugar 7 lbs.
In the search results, from the database, I have the following items (which are each different products):
Sugar, 8 oz .75
Sugar, 1 lb 1.50
Sugar, 2 lb 4.00
Sugar, 5 lb 7.00
Sugar, 10 lb 11.00
Current process:
Get the search results
Check for the cheapest option for any one of the results (only 10 lbs would meet it) or multiples of one of the results (however many of 1 needed to match the criteria, so 7 X 1 lb)
Put the individual product ids into an array
Get the 1:1 permutations, but within that, use some code to add in up to 3 repeats (because 5 lbs + 2 X 1 lbs is the cheapest option, not 5 lbs + 2 lbs)
In this, check for different quantity unit (oz vs lb) and be able to convert and compare
Compare for cheapest and return cheapest
I'm even disregarding where there are more than 6 elements in the permutation to weed out unlikely options and reduce overhead
This works FINE unless there are > 9 product ids (for some I have > 25), then I get this error in the logs (and a completely unrelated error in the browser): Premature end of script headers
This is a lot of code. I'm sorry, just want to be thorough! Is there a more efficient/effective way?
function processCombos($arr, $qty_search, $qty_unit_search){ //$arr is the dataset, $qty_search is 7, $qty_unit_search is 1 for lbs
$combo=array();
$pid_arr = arrayifyProductIDs($arr);
$count = count($pid_arr);
$members = pow(2,$count);
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1'){
$out[] = $pid_arr[$j];
}
}
$minLength=2;
$out_max = count($out);
if ($out_max >= $minLength) {
// now add in different repeats of each of them
$repeat_max = 3;
$indiv = array();
for($k=0;$k<$out_max;$k++){
$tmp = array();
for ($r = 0; $r < $repeat_max; $r++) $tmp[$r] = array_fill(0, $r + 1, $out[$k]);
$indiv[] = $tmp;
}
$x_ct = count($indiv[0]);
$y_ct = count($indiv[1]);
$z_ct = count($indiv[2]) > 0 ? count($indiv[2]): 0;
$perm = array();
for($x=0;$x<$x_ct;$x++){
for($y=0;$y<$y_ct;$y++){
if($z_ct > 0){
for($z=0;$z<$z_ct;$z++){
$perm = array_merge($indiv[0][$x],$indiv[1][$y],$indiv[2][$z]);
}
}else{
$perm = array_merge($indiv[0][$x],$indiv[1][$y]);
}
$p=0;
$max_p=count($perm);
if($max_p >=7){
}else{
$product_ids = array();
$qty = 0;
$price = 0;
while($p < $max_p){
$product_id = $perm[$p];
$data = $arr[$product_id];
if(!$data['qty_unit_id'] OR !$data['qty']){continue;} // go to the next one if it doens't have qty or qty_unit
if($data['qty_unit_id'] == $qty_unit_search){
$product_ids[] = $product_id;
$qty += $data['qty'];
$price += $data['price'];
}else{
$unit_to_convert_data = getQtyUnitName($qty_unit_search);
$unit_to_convert = $unit_to_convert_data['abbr'];
$unit_to_convert_type = $unit_to_convert_data['conv_file'];
if($unit_to_convert_type == $data['conv_file']){
if($data['conv_file'] == "Mass"){
$product_conv = new PhpUnitsOfMeasure\PhysicalQuantity\Mass($data['qty'], $data['qty_unit']);
}else{
$product_conv = new PhpUnitsOfMeasure\PhysicalQuantity\Volume($data['qty'], $data['qty_unit']);
}
$data['qty_CONV'] = number_format($product_conv->toUnit($unit_to_convert),3,".",",");
$product_ids[] = $product_id;
$qty += $data['qty_CONV'];
$price += $data['price'];
}
}
$p++;
}
if(count($combo)==0 AND $qty >= $qty_search){
$combo = array('product_ids' => $product_ids, 'qty' => $qty, 'price' => $price);
}elseif(($qty >= $qty_search AND $price < $combo['price']) OR
($qty >= $qty_search AND $price == $combo['price'] AND $qty > $combo['qty'])){
$combo = array('product_ids' => $product_ids, 'qty' => $qty, 'price' => $price);
}else{
}
}/// i think it should go here
}
}
}
}
return $combo;
}
Premature end of script headers usually means your script is killed before sending headers to the web server due to a resource limit in RLimitCPU and RLimitMEM directives in the httpd.conf. Either change these directives to allow more CPU and memory to your applications or write more efficient code (3 for loops means processing record^3 times the block inside it. consider rewriting it.)
I have a tricky question and would appreciate some suggestions.
A customer sets a price, then that price determines the number of customers that can purchase the item.
I think im looking to have an item price set from a form, then carry out a calculation in a loop, hold this in an array then echo out only whole numbers from the array to an options box which is populated by ajax onlclick.
At least I think thats how to do it.
E.G
itemPrice=50, therefore only the following no of customers can purchase the item.
50,25,10,5,2 or 1 as these are the only whole numbers from the calculation (this should be offered as a combo box or slider).
$itemprice = 50
for ($i=1, $i < $itemprice, ++$i) {
$array .= $i
}
for each {
is_int($array){
$NoOfCustomersList .= <option value="$NoOfCustomers">$NoOfCustomers</option>
//(or jQuery slider)
}
}
DISCLAIMER
At least this is how it would look in my mind, I am new to php so the above may be jibberish
If you mean you want only the numbers that divide 50, you can do this
$itemPrice = 50;
$array = array();
for ( $i = 1; $i <= $itemPrice; $i++ ){
if ( $itemPrice % $i == 0 ){ $array[] = $i; }
}
foreach( $array as $item ){
echo '<option value="', $item ,'"/>', $item ,'</option>';
}
I have a requirement where users are forced to choose the multiple of (n) quantity of a product.
The (n) value is set with each product that can be any number.
customer can only purchase the quantity of product in the multiple of (n) quantity set with product.
Suppose if (n) is 5 and user entered quantity as 4 and says Add to Cart. I have to add quantity of that product as 5 automatically.
and if user entered 6 as quantity then I have to add the 10 quantity of that product.
How I go about that?
I am not getting what logic should be applied here.
$entered_quantity = 6;
$suppose_n = 5;
$quantity = ceil($entered_quantity / $suppose_n) * $suppose_n;
echo $quantity;
prints 10
that's not php specific;
what you wonna do is to compute.
ceiling(q / n) * n
where q is the user's quantity,
n is the multiplicity
You could try getting the remainder of the number when dividing by the given n
e.g.:
$n = 5;
$amount = 6; // This would be the input, so replace the 6 with a $_POST/$_GET/etc.
$batches = floor($amount / $n);
$rest = $amount % $n;
if ($rest > 0) {
$batches += 1;
// You could give the user feedback here that they didn't put in a full multiple of $n
}
// $batches now contains the right amount of batches, so to get the total:
$total = $batches * $n;
Ofcourse this can be condensed a lot, but this might give a better overview of what happens :).
Try the below function.
function getNextMultipleOfFive($n) {
$tmp=explode('.',($n/5));
if($tmp[1]) {
return ($tmp[0]+1)*5;
}
return $tmp[0]*5;
}
With a do...while loop:
$q = 6; // quantity by user input
$n = 5; // per purchace amount
$i = 0;
if ($q > 0)
{
do
{
$i += $n;
}
while ($i <= $q);
}
echo $i; // 10
Note: not very effective if $q >> $n