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
- discount_status - enabled/disabled
- discount_percent - percent of discount
- discount message - message that will be shown on cart page
- discount message for every product - how much you save
<?xml version="1.0"?> <extension> <id>discount</id> <version>0.1.0</version> <cartversions> <item>0.9</item> <item>1.0</item> </cartversions> <dependencies></dependencies> <phpmodules></phpmodules> <layout></layout> <priority>10</priority> <type>extensions</type> < category>discount</ category> <settings> <item id="discount_status"> <type>checkbox</type> <default_value>0</default_value> </item> <item id="discount_percent"> <type>input</type> <default_value>10</default_value> </item> <item id="discount_message"> <type>input</type> <default_value>We are pleased to annouce september discounts</default_value> </item> <item id="discount_product_message"> <type>input</type> <default_value>Old price: %s, You save: %s</default_value> </item> </settings> </extension>
create main driver PHP file ../extensions/discount/main.php
<?php include('core/discount.php'); <?php if ( !defined ( 'DIR_CORE' )) { header ( 'Location: static_pages/' ); } $languages = array( 'admin' => array( 'discount/discount' ), 'storefront' => array(), ); $templates = array( 'admin' => array(), 'storefront' => array( 'pages/checkout/cart.tpl' ), );
create language setting file ../extensions/discount/admin/language/english/discount/discount.xml
<?xml version="1.0"?> <definitions> <definition> <key>discount_name</key> <value><![CDATA[Discount extension]]></value> </definition> <definition> <key>discount_description</key> <value><![CDATA[discount products extension]]></value> </definition> <definition> <key>discount_percent</key> <value><![CDATA[Discount percent]]></value> </definition> <definition> <key>discount_message</key> <value><![CDATA[Discount message]]></value> </definition> <definition> <key>discount_product_message</key> <value><![CDATA[Product message]]></value> </definition> <definition> <key>discount_status</key> <value><![CDATA[Status]]></value> </definition> <definition> <key>discount_status_0</key> <value><![CDATA[Disabled]]></value> </definition> <definition> <key>discount_status_1</key> <value><![CDATA[Enabled]]></value> </definition> </definitions>
create extension class to handle the work: ../extensions/discount/core/discount.php
<?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:
../extensions/discount/core/discount.php
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
storefront/view/default/template/pages/checkout/cart.tpl
to
extensions/discount/storefront/view/default/template/pages/checkout/
edit cart.tpl
<?php if ($error_warning) { ?> <div class="warning"><?php echo $error_warning; ?></div> <?php } ?>
<?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); after $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( $this->registry->get('config')->get('discount_product_message'), $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 } ?>
<tr> <td colspan="7" align="center"> <?php echo $product['discount_product_message']; ?> </td> </tr>