Created
March 8, 2018 13:15
-
-
Save TakesTheBiscuit/6ca1bb281e4edfb9371551f3bec631fd to your computer and use it in GitHub Desktop.
Discount, give away the least, percent or fixed whichever is lesser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$discount_that_was_probably_applied = quick_calc_discount_used(1.21,10,11.99); | |
print_r($discount_that_was_probably_applied); | |
function quick_calc_discount_used($fixed = 0, $percent = 0, $sell_price) { | |
$gave_away = 0; | |
$method_used = false; | |
$final_sell = 0; | |
$percent_sell = 0; | |
$percent_giveaway = 0; | |
if ($percent) { | |
$percent_sell = sprintf("%.2f", ($sell_price * (1-($percent/100)))); | |
//echo $percent_sell; | |
$percent_giveaway = $sell_price - $percent_sell; | |
//echo ' {'.$percent_giveaway; | |
} | |
// -- fixed | |
$fixed_sell = 0; | |
$fixed_giveaway = 0; | |
if ($fixed) { | |
$fixed_sell = $sell_price - $fixed; | |
//echo $fixed_sell; | |
$fixed_giveaway = $sell_price - $fixed_sell; | |
//echo ' {'.$fixed_giveaway; | |
} | |
// -- where did we win? -- give away least theory | |
if ($percent_giveaway > $fixed_giveaway) { | |
$final_sell = $fixed_sell; | |
$gave_away = $fixed_giveaway; | |
$method_used = 'fixed'; | |
} else { | |
$final_sell = $percent_sell; | |
$gave_away = $percent_giveaway; | |
$method_used = 'percent'; | |
} | |
// -- fail safe | |
if ($final_sell < 0.01) { | |
// cant do that | |
$final_sell = $sell_price; // reset with no discount | |
} | |
return compact('gave_away','method_used','final_sell'); | |
} | |
?> |
Author
TakesTheBiscuit
commented
Mar 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment