Basic examples

You need to control Controller data property to change data that passed to template file . We will place hooks in two places

  • in the very beginning of method - to init some data
  • before call to $this->render() to update data
// in the beginning of file
//use to init controller data
$this->extensions->hk_InitData($this);
.....
//some code
.....
//use to update controller data
$this->extensions->hk_UpdateData($this);

// an extension that use these hooks could look like this
<?php
class ExtensionName extends Extension {

   public function onClassNameMethodName_InitData()
   {
   	// add message variable
   	$this->baseObject->data['message'] = $message;
   }

   public function onClassNameMethodName_UpdateData()
   {
   	// update message variable
   	$this->baseObject->data['message'] = ucfirst($message);
   }
}


If you need to create a hook for some method of a class, for example you need to hook to Cart::getProducts method you can do following:

  • you can update all $this->cart->getProducts calls to $this->extensions->hkgetProducts($cart);
  • or you can change original function
//first change original function
public function getProducts() {
   	$registry = Registry::getInstance();
   	// call extensions api
   	return $registry->get('extensions')->hk_getProducts($this);
   }

// rename the original function, now it will be called
// by extensions api if not overrided
public function _getProducts() {}

and call function as
$this->extensions->hk_getProducts();

Hooks Template Variables

In order to add new data (HTML or just text) to template (.tpl) files from extension AbanteCart allows hook template variables that are placed in .tpl files as regular PHP variables. 
Template Hook Variables look like this: <?php echo $this->getHookVar('hook_name'); ?>
These Template Hook Variables can be used in the extension. To add variable use $this->view>addHookVar() method. To locate appropriate template hook variable look into the template file, where you want to add values too.

example
// an extension that access  and modify template hook variables:
<?php
class ExtensionName extends Extension {
   public function onClassNameMethodName_UpdateData()
   {
	// Set new value with appending  new data to $hook_message
	$this->baseObject->view->addHookVar('hook_message', 'Confirmation Massage');
   }

 }

help

Can not add or change values in the base template file or controller? No Problem. Contact us in a forum.abantecart.com and we will help to find solution. If you discover that you need to add something to base template files in AbanteCart storefront or admin, and there is no way to do this or no hook variable available let us know. We can add hooks to current development version and next release.