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;
}
Related
I have an extension. Customer discount. I want to see the discounted product price at the cart and at the bill. How can I do it?
see what I want
<?php
class ModelExtensionTotalCustomerDiscount extends Model {
public function getTotal($total) {
if ($this->cart->getSubTotal() > 0) {
$this->load->language('extension/total/customer_discount');
$price = 0;
$special->row['discount'] = 0;
if(isset($this->session->data['customer_id']) && $this->session->data['customer_id'] > 0) {
$special = $this->db->query("SELECT discount FROM " . DB_PREFIX . "customer WHERE customer_id = '".(int)$this->session->data['customer_id']."'");
if(isset($special->row['discount']) && $special->row['discount'] > 0) {
$price = ($special->row['discount'] / 100) * $this->cart->getSubTotal();
}
}
if ($price > 0) {
$total['totals'][] = array(
'code' => 'customer_discount',
'title' => sprintf($this->language->get('text_discount'),$special->row['discount'].'%'),
'value' => - $price,
'sort_order' => $this->config->get('total_customer_discount_sort_order')
);
$total['total'] -= $price;
}
}
}
}
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);
How can i apply 5% discount in base price after Quantity cross 15 in custom e Commerce.
Code:
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount'];
$tax = $_POST['tax'];
$shipping = $_POST['shipping'];
$payments = $_POST['payments'];
$total = (($price * $quantity) + $shipping) - $discount;
$price = mysqli_real_escape_string($conn , $_POST['price']);
$quantity =mysqli_real_escape_string($conn , $_POST['quantity']);
if ($quantity > 15)
{
$discount = ($price/100)*5;
$price = $price - $discount;
}
else
{
$price = $price;
}
I have a function to calculate price as below. Im just interested to learn any other way to shorten my code.
PHP
function calculate_price( $quantity, $target_quantity, $price, $rate )
{
$total = 0;
if( $quantity > $target_quantity )
{
$total = $target_quantity * $price;
$quantity -= $target_quantity;
$tmp_price = $price;
do {
$tmp_price = $tmp_price * $rate;
$total += $tmp_price;
$quantity--;
} while( $quantity > 0 );
}
else
{
$total = $quantity*$price;
}
return $total;
}
echo calculate_price( 8, 5, 3, 0.5 );
You could combine the decrement in the while statement.
while( --$quantity > 0 );
trying to check whether the quantity entered by user on form is greater than that in stock. if it is, then it should set quantity to STOCK VALUE on the form.
if (isset($_SESSION["cart_array"]))
{
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >= 100) { $quantity = 99; }
if ($quantity < 1) { $quantity = 1; }
if ($quantity == "") { $quantity = 1; }
$sqlquan = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sqlquan)) {
$item_id= $row["id"];
$stock= $row["stock"];
}
if ($quantity > $stock)
{echo "much";
$quantity=$stock;}
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $item_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
}
}
The problem is I have 2 items in the cart. The 1st one's stock value is 15 and the second one's stock value is 23. If i enter a quantity greater than 15 for the first product, it works well(sets quantity to 15). But if i enter a value greater than 15 but less than 23 for the second product, it compares against the stock value of the first product and resets to 15 again.
Cleaned your code quite a bit. Hope this works.
if(isset($_SESSION["cart_array"])) {
if(isset($_POST['item_to_adjust']) && !empty($_POST['item_to_adjust'])) {
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = preg_replace('#[^0-9]#i', '', $_POST['quantity']); // filter everything but numbers
if ($quantity >= 100) $quantity = 99;
if (empty($quantity) || $quantity < 1) $quantity = 1;
$sqlquan = mysql_query("SELECT * FROM products WHERE id='$item_to_adjust' LIMIT 1"); // You previously used id='$item_id', I think you meant $item_to_adjust
// Since you have limited your query to only have 1 result, there is no need to use a while loop
// also note in your original code the while loop was closed prematurely.
$row = mysql_fetch_array($sqlquan);
// No need to create new variables when $row is accessible directly
if($quantity > $row["stock"]) {
echo "Requested Quantity exceeds currently available Stock.<br>";
$quantity = $row["stock"];
echo "Quantity set to maximum available stock of ".$row["stock"]."<br>";
}
foreach($_SESSION["cart_array"] as &$each_item) { // Referenced variable &$each_item so you do not have to use complicated array functions
if($each_item['item_id'] == $item_to_adjust) {
$each_item['quantity'] = $quantity;
break; // Terminates the foreach loop
}
} // close foreach loop
} else {
echo("Invalid Item ID");
}
}