Sale Controller

Hi all, one of the websites I have built for an online store want the option to either add a site-global sale or individual products on sale - both will never be run at the same time, it will be either or.

For this particular site, the user has to make an application so these checks need to be ran at several intervals, when the user view the product page and when entering the application to the database.

Not sure on the best way to do this. Guess it would be a case of building a function or something, any pointers would be appreciated :slight_smile:

Here is a bit of code that I’ve done so far…

//Global sale...
				if($global_sale === true):
					$row .= pretty_price_decimal($pricing['products_price'], $bag->sale_price_calculator($pricing['products_price'], $global_fetch['sale_deduct'], $global_fetch['sale_type']), true);
				//Local product sale...
				elseif($pricing['products_sale_price'] !== NULL):
					$row .= pretty_price_decimal($pricing['products_price'], $bag->sale_price_calculator($pricing['products_price'], $pricing['products_sale_price'], $pricing['sale_type']), true);
				//No sale on...
					$row .= pretty_price_decimal($pricing['products_price']);

function pretty_price_decimal($cost, $sale_price = false){
				$decimal = explode('.', number_format($cost,2));
					$format = ($sale_price !== false) ? '<span class="mediumProduct">was</span> <strike>' : '';
					$format .= '&pound;<span class="priceShoutout">'.$decimal[0].
					'</span><span class="mediumProduct">.'.$decimal[1].'</span>';
					$format .= ($sale_price !== false) ? '</strike>' : '';
					if($sale_price !== false):
						$format .= '<span class="mediumProduct nowSale">now</span> ';
						$sale_decimal = explode('.', $sale_price);
						echo $sale_price;
						$format .= '<span class="priceShoutout red">&pound;'.$sale_decimal[0].
						'</span><span class="mediumProduct red">.'.$sale_decimal[1].'</span>';
					return $format;

public function sale_price_calculator($price, $remove_amount, $type){
				$final_sale_price = ($price / 100) * $remove_amount;
				$final_sale_price = ($price - $final_sale_price);
				return number_format($final_sale_price, 2);
				$final_sale_price = ($price - $remove_amount);
				return number_format($final_sale_price, 2);