Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Creating a custom controller

ktwbc edited this page Jul 31, 2012 · 27 revisions

Creating a custom controller

Some customers may wish to perform additional functionality beyond Web Store by use of a custom controller. Most controllers in Web Store are in the xlsws_includes folder, with customer-created controllers in the custom_includes folders. Web Store will look in both locations for these files.

For this example, we will assume that you wish to create a custom controller that will had a product to a cart. This may be useful for an email campaign, for example, where a customer can click a button from an email and be taken to Web Store with the cart pre-populated with an item.

Controllers in Web Store

Each controller is a flle in xlsws_includes. In most cases, the URL contains the name of the controller to be used. For example, the URL

 http://www.example.com?xlspg=checkout

will look for the checkout controller. The filename must match this word, so the actual filename is checkout.php. Furthermore, the actual classname must be xlsws_ followed by the controller name, such as:

 class xlsws_checkout extends xlsws_index 

Existing controllers can be extended, but in this case will be creating our own controller to perform a very specific function. Because we're not replacing any existing functionality, we will be creating a new controller from scratch.

Example: Creating a controller for a single function

Our example controller will add an item to cart that has been passed along a URL, then redirect the customer to the cart viewing page. We will pretend that we have sent email to customers with a "Click here to order a Widget" button. In our email, we will construct a URL to our store that looks like this:

 http://www.example.com?xlspg=specialadd&product=ABC123&qty=1

This sample URL uses a controller we will develop called specialadd, followed by other parameters we will need such as the Product Code and the Quantity.

We will create a blank file in our /custom_includes folder for our controller. All custom controllers should reside in this folder so upgrades of Web Store don't conflict with our custom files.

 <?php


  class xlsws_specialadd extends xlsws_index {


protected function Form_PreRender() {

	//We get any variables we expect on the command line
	//  http://www.example.com?xlspg=specialadd&product=ABC123&qty=1
	$strProduct = QApplication::QueryString('product');
	$intQty = QApplication::QueryString('qty');

	//Load the current cart (or create it if it doesn't exist)
	$objCart = Cart::GetCart();

	//Get the product we were passed on the command line
	$objProduct = Product::LoadByCode($strProduct);

	//If it's a valid product code, add it to the cart
	if($objProduct)
		$objCart->AddToCart($objProduct , $intQty);

	//Redirect to the cart page
	_rd("index.php?xlspg=cart");
}
 }

 xlsws_specialadd::Run('xlsws_specialadd', templateNamed('index.tpl.php'));

This is our controller. As stated, Web Store controllers should be prefixed with xlsws_ and our controller name, so we have written xlsws_specialadd to match our xlspg= variable on the command line.

We are using the Form_PreRender() function because this is the first function called by Web Store when a controller is loaded. Because we are adding an item to cart and then redirecting, we don't need anything to display on the screen, we can simply go to the cart page and let Web Store show it's default page.

Parse the URL

We can use qCodo's functions to get items passed on the command line. For example, the command $strProduct = QApplication::QueryString('product'); will get the product variable from the URL http://www.example.com?xlspg=specialadd&product=ABC123&qty=1.

Clone this wiki locally