Sometimes you need to modify a system to have it work a certain way for a certain customer. Maybe every time this particular customer buys one widget we actually credit their account with two widgets.

So how do you go about modifying your code to make that happen? You could just write in the code:

if($customer=="greedy_guys") {
	$numWidgets = $numWidgets * 2;
}

Which works fine. But then what if your boss wants an email notification to go out as well when this happens? Then your code becomes:

if($customer=="greedy_guys") {
	$numWidgets = $numWidgets * 2;
	$to = 'myboss@mycompany.com';
	$from = 'thesystem@mycompany.com';
	$subject = 'Greedy Guys got more widgets';
	$body = 'Hello Boss,';
	$body .= '<br><br>Just letting you know that Greedy Guys';
	$body .= 'Got '.$numWidgets.' for the price of '.$numWidgets/2;
	$body .= '<br><br>--<br>The System';
	email_send($to,$from,$subject,$body);
}

So then the boss works out a new deal and now they’re only going to get 2 widgets for the price of 1 if there’s some other situation that occurs, otherwise they get some other complicated formula. So you code becomes:

if($customer=="greedy_guys") {
	$originalNumWidgets = $numWidgets;
	if($thisScenario===true) {
		$numWidgets = $numWidgets * 2;
	} else {
		$numWidgets = $numWidets + round((dateFormat('now','j') % 6),0);
	}
	$to = 'myboss@mycompany.com';
	$from = 'thesystem@mycompany.com';
	$subject = 'Greedy Guys got more widgets';
	$body = 'Hello Boss,';
	$body .= '<br><br>Just letting you know that Greedy Guys';
	$body .= 'Got '.$numWidgets.' for the price of '.$originalNumWidgets;
	$body .= '<br><br>--<br>The System';
	email_send($to,$from,$subject,$body);
}

Now let’s say you’re making all of these changes and then some other changes need to happen on this same page that are critical that have nothing to do with this client but the changes you’ve made for this client haven’t been fully approved yet. What do you do?

Use included files and subcommands.

In our original file, let’s call it “checkout.php”, your code becomes:

if($customer=="greedy_guys") {
	$subcommand = "process_checkout";
	include 'customizations/greedy_guys/greedy.inc.php';
}

In customizations/greedy_guys/greedy.inc.php you have:

if($subcommand=='process_checkout') {
	if($customer=="greedy_guys") {
		$originalNumWidgets = $numWidgets;
		if($thisScenario===true) {
			$numWidgets = $numWidgets * 2;
		} else {
			$numWidgets = $numWidets + round((dateFormat('now','j') % 6),0);
		}
		$to = 'myboss@mycompany.com';
		$from = 'thesystem@mycompany.com';
		$subject = 'Greedy Guys got more widgets';
		$body = 'Hello Boss,';
		$body .= '<br><br>Just letting you know that Greedy Guys';
		$body .= 'Got '.$numWidgets.' for the price of '.$originalNumWidgets;
		$body .= '<br><br>--<br>The System';
		email_send($to,$from,$subject,$body);
	}
}

Now you can go nuts with the customizations for that client and not have it muck up your original checkout page so much.

Important takeaways here: Use included subcommands like this if you need to modify existing page variables on the original system inline and/or have customizations that lend themselves well to this style of coding.

August 20th, 2013

Posted In: Controllers, Syntax

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: