Extension Process Overview

This is an overview of extension development for AbanteCart 

How extensions are processed?

Once AbanteCart page/response load starts (init.php), all enabled extensions are loaded. At this time, system has created instances of all extensions classes responsible for hooks processing. These are core hook files located inside of each extension core directory.
Now, control is passed to Action class which is trying to define controller and method based on route (rt) requested in URL. Action class checks the requested route, and if it is matching the existing extension. If there is a match, extension controller class is loaded.
After extension controller is loaded, requested method is executed and it is ready to render template. System is looking for template file specified. 

  • first it looks for an override file - it will be named as template.override.tpl - any extensions can override template file. But if several extensions tries to override one file, file from extension with higher priority will be used.
  • if override is not found it is looking for template.pre.tpl and template.post.tpl - their content added before and after original template - any extensions can use pre and post files. if several extensions contain pre or postfiles - they all will be rendered according to extension priority
  • at last it is looking for template.tpl

How to develop extensions?

Let us show how to develop extension based on the example that will apply discount on all products

Extension unique id name will be - discount

  • create directory ../extensions/discount
  • create config file ../extensions/discount/config.xml

At minimum we need to add following settings

  1. discount_status - enabled/disabled
  2. discount_percent - percent of discount 
  3. discount message - message that will be shown on cart page 
  4. discount message for every product - how much you save
<?xml version="1.0"?>
   < category>discount</ category>
 <item id="discount_status">
       <item id="discount_percent">
       <item id="discount_message">
           <default_value>We are pleased to annouce september discounts</default_value>
       <item id="discount_product_message">
           <default_value>Old price: %s, You save: %s</default_value>

create main driver PHP file ../extensions/discount/main.php

if ( !defined ( 'DIR_CORE' )) {
	header ( 'Location: static_pages/' );

$languages = array(
  	'admin' => array(
  	'storefront' => array(),

$templates = array(
  	'admin' => array(),
  	'storefront' => array(

create language setting file ../extensions/discount/admin/language/english/discount/discount.xml

<?xml version="1.0"?>
   <value><![CDATA[Discount extension]]></value>
   <value><![CDATA[discount products extension]]></value>
   <value><![CDATA[Discount percent]]></value>
   <value><![CDATA[Discount message]]></value>
   <value><![CDATA[Product message]]></value>

create extension class to handle the work: ../extensions/discount/core/discount.php

class ExtensionDiscount extends Extension {
   protected $registry;
   public function  __construct() {
   	$this->registry = Registry::getInstance();


add a message about discounts to CheckoutCart step/page 1. Edit file add this function below:

public function onControllerPagesCheckoutCart_InitData() {
$discount_message = $this->registry->get('config')->get('discount_message');
	$this->baseObject->view->addHookVar('additional_message', $discount_message);

Extension will need to override default cart template to add discount message copy

edit cart.tpl

<?php if ($error_warning) { ?>
	<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
Insert after it
<?php echo $this->getHookVar('additional_message'); ?>

now lets apply a discount to each product in cart

1. edit core\lib\cart.php

// change original function to support hooks
public function getProducts() {
   $registry = Registry::getInstance();
   return $registry->get('extensions')->do_getProducts($this);

// rename original function
public function _getProducts() {

2. edit extensions\discount\core\discount.php


// use hook added for getProducts method to apply discount
  	public function onACart_getProducts($products) {
       $discount = $this->registry->get('config')->get('discount_percent');
       foreach ( $products as $key => $p ) {
           $products[$key]['original_price'] = $p['price'];
           $products[$key]['original_total'] = $p['total'];
           $products[$key]['price'] = $p['price'] - $p['price'] * $discount / 100;
           $products[$key]['total'] = $p['total'] - $p['total'] * $discount / 100;

3. edit storefront\controller\checkout\cart.php add these lines

//use to update product data before render
$this->extensions->do_UpdateProduct($this, $result);

$this->data['product'] = array(

4. edit extensions\discount\core\discount.php

Use updateData hook. Each controller has this hook and can update its data before render output

public function onControllerPagesCheckoutCart_UpdateData() {
   	$controller = $this->baseObject;
  	   $products = $this->registry->get('cart')->getProducts();
   	$data = $controller->view->getData();
  	   $currency = $this->registry->get('currency');
  	   $tax = $this->registry->get('tax');
  	   $config_tax = $this->registry->get('config')->get('config_tax');
  	 foreach ( $data['products'] as $key => $p ) {
       	$product = $products[$p['key']];
  	       $data['products'][$key]['discount_product_message'] = sprintf(
              	   $currency->format($tax->calculate($product['original_price'], $product['tax_class_id'], $config_tax)),
              	   $currency->format($tax->calculate($product['original_price'] - $product['price'], $product['tax_class_id'], $config_tax))

   	$controller->view->assign('products', $data['products']);


5. edit extensions/discount/storefront/view/default/template/pages/checkout/cart.tpl

<?php foreach ($products as $product) { ?>
<?php } ?>
And insert before <?php } ?>
   <td colspan="7" align="center">
<?php echo $product['discount_product_message']; ?>