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;
}
Related
I have calculated a different custom order total in Woocommerce:
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
$orderproduct = $order->get_items();
$tax_rate = WC_Tax::get_rates( $orderproduct );
if ($tax_rate == "10") {
$percent10 = $total * $tax_rate;
}
if ( $tax_rate == "4" ){
$percent4 = $total * $tax_rate;
}
## -- fai check e calcoli -- ##
$new_total = $total + $percent4 + $percent10; // <== Fake calculation
// imposta un calcolo nuovo
$order->set_total( $new_total );
}
But my calculations don't work and I'm not able to make it for instance.
Any advice or help please?
Is there ever a time you would have both tax rates? Otherwise, I would think one variable for taxes would be enough, like so:
if ($tax_rate == "10") {
$tax_total = $total * $tax_rate;
}
if ( $tax_rate == "4" ){
$tax_total = $total * $tax_rate;
}
## -- fai check e calcoli -- ##
$new_total = $total + $tax_total
Also, what do you mean your calculations are working?
I think you didn't initiate the variables $percent10 and $percent4
try this:
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
$orderproduct = $order->get_items();
$tax_rate = WC_Tax::get_rates( $orderproduct );
$percent10 = 0; // if $tax_rate != "10", will sum zero in $new_total
$percent4 = 0; // if $tax_rate != "4", will sum zero in $new_total
if ($tax_rate == "10") {
$percent10 = $total * $tax_rate;
}
if ( $tax_rate == "4" ){
$percent4 = $total * $tax_rate;
}
## -- fai check e calcoli -- ##
$new_total = $total + $percent4 + $percent10; // <== Fake calculation
// imposta un calcolo nuovo
$order->set_total( $new_total );
}
Because you used the wrong parameter of tax function. You can try this
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
$orderproduct = $order->get_items();
foreach($orderproduct as $product){
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
if($tax_rate = $tax_rates[1]['rate']){
if ($tax_rate == 10) {
$percent10[] = $total * $tax_rate;
}
if ($tax_rate == 4) {
$percent4[] = $total * $tax_rate;
}
}
}
/* */
## -- fai check e calcoli -- ##
$new_total = $total + floatval((is_array($percent4))? array_sum($percent4): 0) + floatval((is_array($percent10))? array_sum($percent10): 0); // <== Fake calculation
// imposta un calcolo nuovo
$order->set_total( $new_total );
}
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);
I got a problem with the following script, which is listing all products from a session. There are products with different $shipping_cost (0.7 or 6.99).
If I have in my card:
products with product_code<100 = shipping_cost should be 0.7,
products with product_code>100 = shipping_cost should be 6.99,
products with product_code<100 AND product_code>100 = shipping_cost keep 6.99.
<?php
if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){
$total = 0;
$list_tax = '';
$gs = 0.70;
$cart_box = '<ul class="view-cart">';
foreach($_SESSION["products"] as $product){ //Print each item, quantity and price.
$product_name = $product["product_name"];
$product_qty = $product["product_qty"];
$product_price = $product["product_price"];
$product_code = $product["product_code"];
$item_price = ($product_price * $product_qty); // price x qty = total item price
$cart_box .= "<li class=\"view-cart-total\">$product_code – $product_name – Anzahl : $product_qty = <strong>" .number_format($item_price, 2, ",", "."). " ".$currency."</strong></li>";
$subtotal = ($product_price * $product_qty); //Multiply item quantity * price
$total = ($total + $subtotal); //Add up to total price
}
if($product_code < 100){
$shipping_cost = $gs;} //Gutschein-Versandkosten
elseif($product_code > 100){
$shipping_cost = $shipping_cost;} //Gutschein-Versandkosten
else {$shipping_cost = $shipping_cost;}
$grand_zw = number_format($total, 2, ",", "."); //Zwischensumme
$grand_total = $total + $shipping_cost; //Gesamtbetrag
foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = ($grand_total - ($grand_total / 119 * 100)); //MwSt
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + 0;
}
foreach($tax_item as $key => $value){ //taxes List
$list_tax .= $key. ' ' .number_format($value, 2, ",", "."). ' '.$currency. '<br />';
}
$grand_netto = ($grand_total - $tax_amount);
$shipping_cost = ($shipping_cost)?'Versandkosten = '. number_format($shipping_cost, 2, ",", ".").' '.$currency.'<br />':'';
//Print Shipping, VAT and Total
...
?>
Problem is, if I have both products (product_code<100 and product_code>100) in the card, it regards only the last product I've given to the card.
So $shipping_price is either 0.7 or 6.99.
You have to put the shipping cost logic inside the foreach loop, this would be a possibility:
$shipping_cost = 0.7;
$total_products_number = 0;
foreach($_SESSION["products"] as $product) {
... here is your existing code of the foreach loop
if($product_code > 100) {
$shipping_cost = 6.99;
}
$total_products_number += $product["product_qty"];
}
if($total_products_number>1) {
$shipping_cost = 6.99;
}
That way the default shipping costs are 0.7 and are only changed to 6.99 if there is a product with $product_code > 100 or if there is more than one product in quantity.
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 thought I had this syntax right, but apparently I don't... here's what I have.
/* Price Calculation */
if ($stage="1") $price + 1000 = $price;
if ($stage="2") $price + 2500 = $price;
if ($stage="3") $price + 5000 = $price;
if ($chrome=on) $price + 100 = $price;
if ($bov=on) $price + 200 = $price;
if ($controller=on) $price + 500 = $price;
if ($filter=on) $price + 50 = $price;
print "<br /><u>Total: </u>";
echo $price;
The variables are already declared correctly, so I know it isn't that. The php has a problem with the ='s. What did I do wrong?
you should use == in condition
if ($stage="1")
should be
if ($stage=="1")
and adding should be
$price = $price + 1000;
Your code should be
<?php
/* Price Calculation */
if ($stage=="1") {
$price = $price + 1000;
}
if ($stage=="2") {
$price = $price + 2500;
}
if ($stage=="3") {
$price = $price + 5000;
}
if ($chrome==true) {
$price = $price + 100;
}
if ($bov==true) {
$price = $price + 200;
}
if ($controller==true) {
$price = $price + 500;
}
if ($filter==true) {
$price = $price + 50;
}
echo "<br /><u>Total: </u>";
echo $price;
?>
I don't know why you are using on. I think, it's a Boolean condition.
if ($stage=="1") $price = $price + 1000;
if ($stage=="2") $price = $price + 2500;
if ($stage=="3") $price = $price + 5000;
if ($chrome==on) $price = $price + 100;
if ($bov==on) $price = $price + 200;
if ($controller==on) $price = $price + 500;
if ($filter==on) $price = $price + 50;
print "<br /><u>Total: </u>";
echo $price;
Shouldn't it be $price = $price + 100; or $price += 100?
Also, you are mistaking your assignment for equality testing. = never meant equality in php---it means assignment (think of it $price = $price + 100 doesn't make sense it it is equality). To test for equality, use == (check your conditions).
You should change your code to:
if ($stage=="1")
$price = $price + 1000;
if ($stage=="2")
$price = $price + 2500;
if ($stage=="3")
$price = $price + 5000;
if ($chrome==on)
$price = $price + 100;
if ($bov==on)
$price = $price + 200;
if ($controller==on)
$price = $price + 500;
if ($filter==on)
$price = $price + 50;
print "<br /><u>Total: </u>";
echo $price;
Hope this helps :)
In addition to using == for comparison (since = is for assignment), consider adding using the += operator to add values to variables. Also, while it works, many folks consider it bad practice to skip your curly braces.
if ($stage=="1") {
$price += 1000;
}
...
if ($stage=="1") $price += 1000;
if ($stage=="2") $price += 2500;
if ($stage=="3") $price += 5000;
if ($chrome=="on") $price += 100; // if your want to use "on" instead of TRUE, use it as a string
if ($bov=="on") $price += 200;
if ($controller=="on") $price += 500;
if ($filter=="on") $price += 50;
echo "<br /><u>Total: </u>";
echo $price;
There are a couple of problems:
(1)
You are trying to assign the value of $price into $price + number, which is impossible. Try:
$price = $price + number;
(2)
Use the == operator for checking equality, like:
if ($stage == "1")
A single = is the assignment operator.