Web Development. Client Satisfaction. Web Consultancy. Professional. UX. Fast. Responsive Design. Dynamic. Interactive. Innovative. Ecommerce. Wordpress plugins. Prestashop modules. You name it.

Create a Custom Plugin For Smarty in Prestashop

Create a Custom Plugin For Smarty in Prestashop

What is actually a custom plugin for smarty in prestashop, is a code syntax defined by you and when smarty find that syntax will call a custom function, the function which you tell what should do. In this tutorial I will show you step by step how to implement this feature from scratch, this functionality will work something like a shortcode but it can be access only from tpl, I mean you can't use this modifier from CMS content editor for instance, if you want  a function that work that way you need to use the tutorial how to create shortcodes in prestashop. As you will see below you will be pass how many parameters as array, and then get all of them in your class or controller. 

First of all we need to register a hook which will run every time when a controller will initialize, to do that we will regiter to following hook in install function of your module.

$this->registerHook('actionDispatcher'); 

ActionDispatcher hook runs every time when a controller is initialized, below is the code which will use for this hook.

public function hookactionDispatcher(){
    $this->context->smarty->registerPlugin('modifier', 'my_function', array('MyModuleController', 'getFunction'));
  }

Now, in MyModuleController file we will have to create a new function, in our case getFunction() and every time when you will use {$array|my_function} this function will be called, the function for mymodulecontroller class.

Very important is that you need to create function as public static function, if not an error will be thrown. Look at the following function:

public static function getFunction($array){
   echo '<pre>';var_dump($array);die('###');
}

You can pass in the function everything you need, not only arrays. In prestashop .tpl files you can call the plugin like this:

{$param|my_function}

Here is all code you need to implement to create a custom plugin in sparty prestashop. 

Leave a Reply

avatar
  Subscribe  
Notify of