I am trying to create a loop in PHP where for each extra item in a shopping cart, the shipping gets a ascending 10% discount.
This is the result I want:
Quantity = 1: Shipping = 30;
Quantity = 2: Shipping = 30 + (30 x 0.5);
Quantity = 3: Shipping = 45 + (30 x 0.4);
Quantity = 4: Shipping = 57 + (30 x 0.3);
Quantity = 5: Shipping = 69 + (30 x 0.2);
Quantity = 6: Shipping = 78 + (30 x 0.1);
Quantity = 7: Shipping = 81 + (30 x 0.1);
Quantity = 8: Shipping = 84 + (30 x 0.1);
etc.
I don't know where to place my $quantity variable now.
My PHP:
<?php
$quantity = 10;
$shipping = 30;
for( $discount = 0.5; $discount >= 0.1; $discount - 0.1 ) {
$shipping = $shipping + ( $shipping * $discount );
}
echo $shipping;
?>
you should iterate the quantity value, not the discount.
$shippingDefault = 30;
$discount = 0.5;
for ($i = 2 ; $i <= $quantity; $i++) {
if ($discount < 0.1) {
$discount = 0.1;
}
$shipping += ( $shippingDefault * $discount );
$discount -= 0.1;
}
Related
I have installed a PHP script into the functions.php file and now I need to call it from the price field for import have no clue on which code to use? Below is the function.PHP code\
I am new to this so I am learning have checked 20 allimport help posts but can not connect the dots Thank you in advance XMark
function price_increment( $price ) {
if($price <= 20){
$adjustment = .15;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 50){
$adjustment = .12;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 100){
$adjustment = .07;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else {
$adjustment = .05;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
}
}
I'm trying to import a csv file with wp all import set specific functions to set the sale price and the offer price.
Now I would need to call the function that forms the regular price, inside the function of the offer price, to work on the result and maybe apply a percentage discount.
<?php
$GLOBALS['$commissione_paypal_lm'] = 4;
$GLOBALS['$spese_spedizione_moto_lm'] = 10;
$GLOBALS['$spese_spedizione_autocarro_lm'] = 10;
$GLOBALS['$spese_spedizione_varie_lm'] = 7;
$GLOBALS['$pfu_moto_lm'] = 1.50;
$GLOBALS['$pfu_autocarro_lm'] = 4.90;
$GLOBALS['$pfu_varie_lm'] = 2.90;
// First function for regular price,i take variables from csv and global variables
function prezzo_finale( $price = null, $pfu = null, $diametro = null ) {
if ( !empty( $price ) ) {
// strip any extra characters from price
$price = preg_replace("/[^0-9,.]/", "", $price);
$pfu = preg_replace("/[^0-9,.]/", "", $pfu);
//price ommas and dots fix
$price = str_replace(",",".",str_replace(".","",$price));
$pfu = str_replace(",",".",str_replace(".","",$pfu));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
$prezzo_finale = $price;
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else if ($percent > 0){
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}
// perform calculations
return $prezzo_finale;
}
}
function prezzo_finale_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null ) {
if ( !empty( $fifo_ponderato ) ) {
// strip any extra characters from price
$fifo_ponderato = preg_replace("/[^0-9,.]/", "", $fifo_ponderato);
//price ommas and dots fix
$fifo_ponderato = str_replace(",",".",str_replace(".","",$fifo_ponderato));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
// moto = 10; vettura 8; isole extra valutare; paypal 3%;
$prezzo_finale_lm = ($fifo_ponderato);
if ($settori_codice === 'MOTO' || $settori_codice === 'SCOOTER' || $settori_codice === 'CICLOMOTORI'){
$spese_spedizione = $GLOBALS['$spese_spedizione_moto_lm'];
$pfu = $GLOBALS['$pfu_moto_lm'];
}else if ($settori_codice === 'AUTOCARRO'){
$spese_spedizione = $GLOBALS['$spese_spedizione_autocarro_lm'];
$pfu = $GLOBALS['$pfu_autocarro_lm'];
}else{
$spese_spedizione = $GLOBALS['$spese_spedizione_varie_lm'];
$pfu = $GLOBALS['$pfu_varie_lm'];
}
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else if ($percent > 0){
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}
// perform calculations
return $prezzo_finale_lm_paypal;
}
}
// Second function for offer price,i would take regular price and work with
function prezzo_offerta_lm() {
if (prezzo_finale_lm()){
return prezzo_finale_lm();
// and for example apply discount as global variable
}else{
}
// perform calculations
}
?>
This is shortcode i use for regular price:
[prezzo_finale_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
Thank you all for any advice.
<?php
// First function for regular price,i take $price variable from imported csv column
function regular_price( $price = null) {
if ( !empty( $price ) ) {
// final price
$prezzo_finale = $price;
// perform calculations as example add markup or paypal commission
return $prezzo_finale;
}
}
// Second function for offer price,where i need to get returned value from first function
function offer_price($price = null)) {
if ( !empty( $price ) ) {
$offer_price = regular_price(); // i need to return here value from first function to perform discount calculation
// perform calculations
return $offer_price;
}
}
?>
Let's say we have a code:
<?php
$maximum = 10000;
$minimum = 5000;
$size = 10000;
$step = 0.0001;
$total = 0;
$counter = $maximum;
while($counter >= $minimum)
{
$counter -= $step;
$total += $size / $maximum * $step / $counter;
}
echo "Total is: $total\n";
?>
How to express $total variable as a formula? The code acts as an integral where step is infinitesimally small.
Your integral is:
$total = integral(x from $minimum to $maximum; $size / $maximum / x);
$size / $maximum is constant and thus simply scales the integral. The closed form of the integral is therefore:
$total = $size / $maximum * (log($maximum) - log($minimum))
= $size / $maximum * log($maximum / $minimum)
Assuming that both $maximum and $minimum are positive.
using this price table we are calculating price of the given input measurement
example:
(1) width = 30; , height =56 ; Price=?
so
(width>25){ new_width=50; }
(height<100){ new_height=100; }
so price = 20;
(2) width =12; height=267;
(width<25){ new_width=25; }
(height>250){ new_height=300; }
so price= 30;
I wrote a function for to calculate price based on the given value, and this function help to get the new_width , and height based on the table , But I don't know how to get price based on this table.
function calculate_price($width,$height ){
if($width<25){
$new_width=25;
}elseif($width>25 && $width<50 ){
$new_width=50;
}else{
$new_width=50;
}
if($width<100){
$new_width=100;
}elseif($width>100 && $width<150 ){
$new_width=150;
}elseif($width>150 && $width<200 ){
$new_width=200;
}elseif($width>200 && $width<250 ){
$new_width=250;
}elseif($width>250 && $width<300 ){
$new_width=300;
}
else{
$new_width=300;
}
$price=needed forumula [ based on new_width & new_height ];
return $price ;
}
You can use ceil to round up the width and height this function
You can use array to store the price with key height_width for simplicity.
function calculate_price($width, $height) {
$price = array(
"100_25" => 10,
"100_50" => 20,
"150_25" => 15,
"150_50" => 25,
"200_25" => 18,
"200_50" => 31,
"250_25" => 24,
"250_50" => 42,
"300_25" => 20,
"300_50" => 45,
);
$newWidth = ceil($width / 25) * 25; //Round up by 25
$newHeight = ceil($height / 50) * 50; //Round up by 50
return $price[ $newHeight . "_" . $newWidth ];
}
You can call
echo calculate_price( 30, 56 ); /* width and height parameters */
This will result to
20
Base on the info you provided, you have two logical paths. The first, is the width less than or equal to 25 or is the width greater then 25. Then you have to reason out the height.
Consider the following:
function calculate_price($width, $height){
$price = false;
if($width > 25){
switch(true){
case $height <= 100:
$price = 20;
break;
case $height <= 150:
$price = 25;
break;
case $height <= 200:
$price = 31;
break;
case $height <= 250:
$price = 42;
break;
case $height <= 300:
$price = 45;
break;
}
} else {
switch(true){
case $height <= 100:
$price = 10;
break;
case $height <= 150:
$price = 15;
break;
case $height <= 200:
$price = 18;
break;
case $height <= 250:
$price = 24;
break;
case $height <= 300:
$price = 30;
break;
}
}
return $price;
}
With the following input: echo calculate_price(30, 56); we would have an output of: 20.
Running the following:
echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";
I get:
20
30
24
Format as needed (E.G.: echo sprintf("$%d.00", calculate_price(30, 56));)
Full Test
<?php
function calculate_price($width, $height){
$price = false;
if(!is_int($width) || !is_int($height)){
return $price;
}
if($width > 25){
switch(true){
case $height <= 100:
$price = 20;
break;
case $height <= 150:
$price = 25;
break;
case $height <= 200:
$price = 31;
break;
case $height <= 250:
$price = 42;
break;
case $height <= 300:
$price = 45;
break;
}
} else {
switch(true){
case $height <= 100:
$price = 10;
break;
case $height <= 150:
$price = 15;
break;
case $height <= 200:
$price = 18;
break;
case $height <= 250:
$price = 24;
break;
case $height <= 300:
$price = 30;
break;
}
}
return $price;
}
echo sprintf("$%d.00", calculate_price(30, 56)) . "<br />";
echo sprintf("$%d.00", calculate_price(12, 267)) . "<br />";
echo sprintf("$%d.00", calculate_price(25, 249)) . "<br />";
echo sprintf("$%d.00", calculate_price(-1, 'a')) . "<br />";
?>
Results:
$20.00
$30.00
$24.00
$0.00
You can use Multidimensional Array
$x = "300";
$y = "50";
$multi = array();
$multi["100"]["25"] = "10";
$multi["100"]["50"] = "20";
$multi["200"]["25"] = "30";
$multi["300"]["50"] = "40";
echo $multi[$x][$y];
Based of your code, if you don't have database to get those $price, I think you can try somehting like this :
function calculate_price($width,$height ){
// Your $new_width according to your $width
if($width<=25){
$new_width=25;
}else
$new_width=50;
}
// Your $new_height according to your $height
if($height<=100){
$new_height=100;
}elseif($height>100 && $height<=150 ){
$new_height=150;
}elseif($height>150 && $height<=200 ){
$new_height=200;
}elseif($height>200 && $height<=250 ){
$new_height=250;
}elseif($height>250){
$new_height=300;
}
// Now, according to your new value you will find the price with some test :
if ($new_width == 25) {
switch($new_height) {
case 100 : $price = 10; break;
case 150 : $price = 15; break;
case 200 : $price = 18; break;
case 250 : $price = 24; break;
case 300 : $price = 30; break;
}
} else {
switch($new_height) {
case 100 : $price = 20; break;
case 150 : $price = 25; break;
case 200 : $price = 31; break;
case 250 : $price = 42; break;
case 300 : $price = 45; break;
}
}
return $price ;
}
But I think you can find other way to achieve it too as suggested in other anwser !
Is this what you are looking for? It's not very flexible since you don't use database to get those value but it should work
I am building a feedback system: positive, neutral and nagative, I want to get the positive percentage somehow.
$positive = 4;
$neutral = 1;
$negative = 1
So I am trying to get something like 50%. I have tried
$positive / $neutral + $negative;
$positive / $neutral + $negative * 10;
None seem to get the right value.
This should work for you:
<?php
$positive = 4;
$neutral = 1;
$negative = 1;
$total = $positive + $neutral + $negative;
$percentage = ($positive - $negative) / $total * 100;
echo sprintf("%02.2f%%", $percentage);
?>
Output:
50.00%
EDIT:
If you want to count $neutral to positive feedback use this:
$total = $positive + $neutral + $negative;
$percentage = ($positive + $neutral - $negative) / $total * 100;
echo sprintf("%02.2f%%", $percentage);