PHP code for function.php, regarding pricing

Hi

Im looking to create a custom PHP function within my wordpress theme’s function.php file.

I run Woocommerce

Basically I need it to be something in the lines of:

When product is uploaded
It takes the “price” field
Checks weather the price given falls between 0 and 500, 501 and 1000, 1001 and 4000, 4000 and 8000 etc
Based on where the price falls, it adds a markup
If between 0 and 500: Then x1.382
If between 501 and 1000: Then x1.311
Etc

Is this possible?

I want to run cron jobs from data sheets received by suppliers. They all give the cost price, so when I pull it in, it displays the cost price on my site.

Any help is appreciated.

Here you go, try this:
[php]
function add_markup($price) {
$percentage = 0;

// let's calculate the percentage based upon the price.
if($price >= 4001 && $price <= 8000) {
	$percentage = 2.5;
}
elseif($price >= 1001 && $price <= 4000) {
	$percentage = 2;
}
elseif($price >= 501 && $price <= 1000) {
	$percentage = 1.311;
}
elseif($price >= 101 && $price <= 500) {
	$percentage = 1.382;
}
else { // between 0 & 100
	$percentage = 0.5;
}

// calculate and return the new total.
return number_format($price + ($price * $percentage) / 100, 2);

}

// call the function.
$price = 720;
$new_price = add_markup($price);
echo $new_price;
// output: 729.44
[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service