I want to make a discounted php formula with a voucher but must have a maximum cashback.
example: 20% discount with a maximum of 40000 cashback.
case: if inputted with the price of 100000 with a 20% voucher, the cashback earned is 20000, but if the inputted price is 500000 with a voucher of 20% also then the cashback is only 40000.
help me to solve it, I have searched Google but found no solution.
I have this script :
my script :
<?php
require_once 'conn.php';
$coupon_code = $_POST['coupon'];
$price = $_POST['price'];
$totalorder = $_POST ['totalorder'];
$query = mysqli_query($conn, "SELECT * FROM `coupon` WHERE `coupon_code` = '$coupon_code' && `status` = 'Valid'") or die(mysqli_error());
$count = mysqli_num_rows($query);
$fetch = mysqli_fetch_array($query);
$array = array();
if($count > 0){
$discount = $fetch['discount'] / 100;
$total = $discount * $price;
$array['discount'] = $fetch['discount'];
$array['price'] = ($price - $total) * $totalorder;
echo json_encode($array);
}else{
echo "error";
}
?>
I hope this is what you expected :
<?php
function voucher_calc($price, $max, $voucher){
$discount = $price * $voucher;
if($discount >= $max){
return $max;
}else{
return $discount;
}
}
$price = 500000;
$voucher = 0.2;
$max = 40000;
echo voucher_calc($price, $max, $voucher);
Related
do you have an idea how to get a percentage as in the picture
my code
$dateFrom = Carbon::now()->subDays(30);
$dateTo = Carbon::now();
$monthly = User::whereBetween('created_at', [$dateFrom, $dateTo])->count();
$previousDateFrom = Carbon::now()->subDays(60);
$previousDateTo = Carbon::now()->subDays(31);
$previousMonthly = User::whereBetween('created_at', [$previousDateFrom, $previousDateTo])->count();
if ($previousMonthly < $monthly) {
if ($previousMonthly > 0) {
$percent_from = $monthly - $previousMonthly;
(int) $percent = $percent_from / $previousMonthly * 100; //increase percent
} else {
(int) $percent = 100;
}
} else {
$percent_from = $previousMonthly - $monthly;
(int) $percent = $percent_from / $previousMonthly * 100;
}
any suggestion?
OK so I have this loop, I want to change the value if the loop lands on a certain value. for example in the code below, $p will equal 15 for Tier2. but when running the script nothing happens, it just runs as normal and ignores my if statement.
$get_sponsor = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT sponsor FROM ap_members WHERE id=$affiliate_id"));
$sponsor = $get_sponsor['sponsor'];
for ($loop = 2 ; $loop < $levels; $loop++){
//CHECK FOR AVAILABLE SPONSOR
if($sponsor!='0'){
//GET LEVEL PERCENTAGE
$gp = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM ap_other_commissions WHERE id=1"));
$p = $gp['tier'.$loop.''];
if($p == 15){ // changes for custom override
$sc = $tier2_com / 100;
} else {
$sc = $p / 100;
}
$se = $sale_amount * $sc;
//GET LEVEL PERCENTAGE
$gp = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM ap_other_commissions WHERE id=1"));
$p = $gp['tier'.$loop.''];
if($loop == 2){ // changes for custom override
$p = $tier2_com;
$sc = $tier2_com / 100;
} else {
$sc = $p / 100;
}
$se = $sale_amount * $sc;
I released that i was missing the $p in the if statement to give to update it correctly in the database. all is working now.. thanks for the suggestions guys.
I trying to develop a function allowing to calculate a specific price in function the quantity discount
Example : 10% = $discount_customer[] in function the database
$qty is add by an user
if $qty < 5 then $$discount_customer[] = 0 : price does'nt change
if $qty > 5 et qty < 10 then $$discount_customer[] = 10% : price with -10%
...
if $qty > 10 then $$discount_customer[] = 15% : price with -15%
$id : id of the product
$qty : order quantity inserted by a customer
$products_price : price of the product
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
// quantity discount
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
// Customer discount
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
I suppose to create a foreach but how ?
Thie element doesn't take the between condition.
foreach ($discount_quantity as $k => $quantity) {
print_r('<pre>');
var_dump($quantity );
print_r('</pre>');
foreach ($discount_customer as $c => $discount) {
if ($qty > $quantity) {
$news_price = $products_price -(($products_price * $discount) /100);
}
print_r('<pre>');
print_r($news_price);
print_r('</pre>');
}
return $newprice
}
thank you
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
return $this->getPrctDiscount($qty, $products_price);
}
public function getPrctDiscount($qty, $products_price)
{
$discount = 0;
if ($qty > 5 && $qty < 10) {
$discount = 10;
}elseif ($qty > 10) {
$discount = 15;
}
$newPrice = $discount > 0
? $products_price - ($products_price * ($discount/100))
: $newPrice;
return $newPrice;
}
I have a function that should calculate prices based on quantity.
The function should loop through every order and calculate every product price based on quantity, then should return order total price.
What i'm doing wrong?
public function getSumaComanda($cos) {
$suma = $this->_db->query(sprintf("SELECT (#pretredus:=`pretredus`) AS `pretredus`,(CASE #pretredus WHEN 0 THEN `prettotal` ELSE `pretredus` END) AS `prettotal` , cantitate FROM comenzi WHERE cos = '%d'", $cos));
$suma->execute();
$data_array = $suma->fetchAll(PDO::FETCH_ASSOC);
$count = $this->_db->query(sprintf("SELECT COUNT(*) FROM cosuri WHERE id='%d'", $cos));
$num = $count->fetchColumn();
for ($x = 0; $x < $num; $x++) {
$price = $data_array['cantitate'][$x] * $data_array['prettotal'][$x];
$pret = $pret + $price;
$pret = number_format($pret, 2, ".", "");
}
$rez = $pret;
return $rez . ' Lei';
}
You should learn how to basically debug your variables. With using var_dump($data_array); you can see, what's in there.
You have to use the numerical index first:
$price = $data_array[$x]['cantitate'] * $data_array[$x]['prettotal'];
Nevertheless, your second query is useless. You don't have to count the results and can use instead a while-loop:
public function getSumaComanda($cos) {
$suma = $this->_db->query(sprintf("SELECT (#pretredus:=`pretredus`) AS `pretredus`,(CASE #pretredus WHEN 0 THEN `prettotal` ELSE `pretredus` END) AS `prettotal` , cantitate FROM comenzi WHERE cos = '%d'", $cos));
$suma->execute();
while ($data_array = $suma->fetch(PDO::FETCH_ASSOC)) {
$pret += $data_array['cantitate'] * $data_array['prettotal'];
}
return number_format($pret, 2, ".", "") . ' Lei';
}
ok so im working with this loop and getting information from DB:
for($i0 = 0; $i0 < $total0; $i0 ++) {
$id = $oconecta->retrieve_value($i0, "id_tam_product");
$price = $oconecta->retrieve_value($i0, "price_tam_product");
echo $id; // RESULTS: 312, 313, 314
echo $price; // RESULTS: 180.00, 250.00, 300.00
}
i was wondering how can i get the MAX value for that loop:
echo $id; //RESULTS: 314
echo $price; //RESULTS: 300.00
$maxID = 0;
$maxPrice = 0;
for($i0=0;$i0<$total0;$i0++)
{
$id=$oconecta->retrieve_value($i0,"id_tam_product");
$price=$oconecta->retrieve_value($i0,"price_tam_product");
$maxID = max($id, $maxID);
$maxPrice = max($price, $maxPrice);
}
echo "MAX ID: $maxID - Max PRICE: $maxPrice";
Use the max() function to determine the highest number of a set.
Either you use SQL's MAX() if you can modify your SQL query, or keep the max value in a variable in each loop, and reassign it everytime:
$firstLoop = true;
$maxId = 0;
$maxPrice = 0;
for ($i0 = 0; $i0 < $total0; $i0++)
{
$id = $oconecta->retrieve_value($i0, "id_tam_product");
$price = $oconecta->retrieve_value($i0, "price_tam_product");
if ($firstLoop) {
$firstLoop = false;
$maxId = $id;
$maxPrice = $price;
}
else {
$maxId = max($maxId, $id);
$maxPrice = max($maxPrice, $price);
}
}
(the boolean is here if you have negative values it wouldn't work if $maxPrice and $maxId are initiliazed with 0)