In this blog, we are going to learn how to register your own smarty helper plugin in PrestaShop. PrestaShop uses a Smarty template engine for the view section. Smarty is open source and provides flexibility for custom development.
PrestaShop has already registered its own helper plugin like displayPrice
, convertPrice
, displayWtPrice
, etc.
We can directly use it in our smarty file.
For example:
{displayPrice price=$price currency=$id_currency}
We will learn how to create and register our own smarty helper plugin.
Smarty provides a method registerPlugin()
which provides the facility to register plugins dynamically. But this method is not directly available in our module, PrestaShop has an alternative function smartyRegisterFunction
that is available globally.
This function takes 6 parameters:
Parameter | Required | Description |
$smarty | Yes | Pass smarty object (ie: $this->context->smarty ) |
$type | Yes | Plugin type: function, modifier, or block |
$function | Yes | Your function name |
$params | Yes | Array of parameters |
$lazy | No | Pass as true if the function is not called on every page |
$initial_lazy_register | No | Allows to only load external classes when they are needed |
Types of plugins in PrestaShop:
There are 3 plugins in the PrestaShop:
- Function
- Modifier
- Block
Function plugin:
All attributes passed to template functions from the template are contained in the $params
as an associative array.
ie:
smartyRegisterFunction( $this->context->smarty, 'function', 'displayProductQuantity', [ 'WkTestClass', 'displayProductQuantity' ] );
Create a WkTestClass
and create a static method displayProductQuantity
:
ie:
class WkTestClass { public static function displayProductQuantity($params) { if (!$params['id_product']) { return false; } return StockAvailable::getQuantityAvailableByProduct((int)$params['id_product']); } }
Now, you can use this function in smarty like below:
{displayProductQuantity id_product='1'} // Print available stock for product ID: 1
Modifier plugin:
Modifiers are little functions that are applied to a variable in the template before it is displayed or used in some other context. Modifiers can be chained together.
smartyRegisterFunction( $this->context->smarty, 'modifier', 'removeSlashes', [ 'WkTestClass', 'removeSlashesFromString' ] );
Create a WkTestClass
and create a static method removeSlashesFromString
:
class WkTestClass { public static function removeSlashesFromString($params) { if (!$params) { return false; } return stripslashes($params); } }
You can use this modifier in smarty like:
{"Who\'s Peter Griffin?"|removeSlashes} // Output: Who's Peter Griffin?
Modifier plugin:
Block functions are functions of the form: {func} .. {/func}
. In other words, they enclose a template block and operate on the contents of this block.
ie:
smartyRegisterFunction( $this->context->smarty, 'block', 'getFormattedProductInfo', [ 'WkTestClass', 'getFormattedProductInfo' ] );
Create a WkTestClass
and create a static method getFormattedProductInfo
:
public static function getFormattedProductInfo($params, $content, $smarty = null, &$repeat = false) { // only output on the closing tag if (!$repeat && isset($params) && Tools::strlen($content)) { $objProduct = new Product((int)$params['id_product'], false, (int)$params['id_lang']); echo sprintf( $content, $objProduct->name, StockAvailable::getQuantityAvailableByProduct((int)$params['id_product']) ); } }
You can use this block in smarty like:
{getFormattedProductInfo id_product='1' id_lang='1'} This product name is %s which current stock is %d. {/getFormattedProductInfo} // Output: This product name is Hummingbird printed t-shirt which current stock is 2397.
Note: In the block plugin, by default, the function getFormattedProductInfo
is called twice by Smarty: once for the opening tag, and once for the closing tag. At the first call, the $repeat
param value is true
but the $content
param is null that’s why I have checked for $repeat
param value to false
because at the second call, this value is false
and the actual $content
value exists.

That’s all about this blog.
If any issue or doubt please feel free to mention it in the comment section.
I would be happy to help.
Also, you can explore our PrestaShop Development Services & a large range of quality PrestaShop Modules.
For any doubt contact us at [email protected].
Be the first to comment.