{"id":441,"date":"2014-07-23T10:31:43","date_gmt":"2014-07-23T10:31:43","guid":{"rendered":"http:\/\/www.confianzit.com\/cit-blog\/?p=441"},"modified":"2023-09-14T11:35:17","modified_gmt":"2023-09-14T11:35:17","slug":"prestashop-module-development","status":"publish","type":"post","link":"https:\/\/www.confianzit.com\/cit-blog\/prestashop-module-development\/","title":{"rendered":"Prestashop Module Development Tutorial"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;4.16&#8243; global_colors_info=&#8221;{}&#8221;][et_pb_row column_structure=&#8221;3_5,2_5&#8243; _builder_version=&#8221;4.16&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_column type=&#8221;3_5&#8243; _builder_version=&#8221;4.16&#8243; custom_padding=&#8221;|||&#8221; global_colors_info=&#8221;{}&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_text module_class=&#8221;blog-left-content&#8221; _builder_version=&#8221;4.21.0&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p><a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-444\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development-300x300.jpg\" alt=\"Prestashop Module Development\" width=\"300\" height=\"300\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development-300x300.jpg 300w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development-150x150.jpg 150w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development.jpg 500w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This article explains how to create a module for prestashop<a href=\"https:\/\/www.confianzit.com\/odoo-prestashop-connector\"><strong><\/strong><\/a>. Let\u2019s create a simple module and will name it \u2018 <strong>My module<\/strong> \u2019.<br \/>First, create a folder for the module in the &lt;HOME_DIRECTORY&gt;\/modules folder and it should have the same name as the module, with no space, only alphanumerical characters, the hyphen and the underscore, all in lowercase:<br \/>(This will be our folder structure: &lt;HOME_DIRECTORY&gt;\/modules\/mymodule.)<\/p>\n<p>This folder must contain the main file, a PHP file of the same name as the folder, which will handle most of the processing: mymodule.php<br \/><!--more--><br \/>The main mymodule.php file must start with the following test:<br \/><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p>&lt;?php<\/p>\n<p><code><\/code><\/p>\n<p>if (!defined(&#8216;_PS_VERSION_&#8217;))<\/p>\n<p><code><\/code><\/p>\n<p>exit;<\/p>\n<p><code><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code><\/code><\/p>\n<p>This checks for the existence of an always-existing prestashop<a href=\"https:\/\/www.confianzit.com\/odoo-prestashop-connector\"><\/a> constant (its version number), and if it does not exist, it stops the module from loading. The sole purpose of this is to prevent malicious visitors to load this file directly.<\/p>\n<p>The main file (mymodule.php) must contain the module&#8217;s main class, and that class must bear the same name as the module and its folder, in CamelCase and that class must extend the Module class, in order to inherit all its methods and attributes. (It can just as well extend any class derived from Module, for specific needs: PaymentModule, ModuleGridEngine, ModuleGraph, etc.)<\/p>\n<p>At this stage, the module can already be seen in the &#8220;Modules&#8221; page in the back-office (in the &#8220;Other modules&#8221; section) \u2013 albeit with no real name nor thumbnail.<\/p>\n<p>Now, let&#8217;s fill the class code block with the essential constructor lines. Our mymodule.php file will look like this now. The use of each attribute in the constructor is explained below.<br \/><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p>&lt;?php<\/p>\n<p><code><\/code><\/p>\n<p>if (!defined(&#8216;_PS_VERSION_&#8217;))<\/p>\n<p><code><\/code><\/p>\n<p>exit;<\/p>\n<p><code><\/code><\/p>\n<p>class MyModule extends Module<\/p>\n<p><code><\/code><\/p>\n<p>{<\/p>\n<p><code><\/code><\/p>\n<p>public function __construct()<\/p>\n<p><code><\/code><\/p>\n<p>{<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;name = &#8216;mymodule&#8217;;<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;tab = &#8216;front_office_features&#8217;;<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;version = &#8216;1.0&#8217;;<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;author = &#8216;Firstname Lastname&#8217;;<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;need_instance = 0;<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;ps_versions_compliancy = array(&#8216;min&#8217; =&gt; &#8216;1.5&#8217;, &#8216;max&#8217; =&gt; &#8216;1.6&#8217;);<\/p>\n<p><code><\/code><\/p>\n<p>parent::__construct();<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;displayName = $this-&gt;l(&#8216;My module&#8217;);<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;description = $this-&gt;l(&#8216;Description of my module.&#8217;);<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;confirmUninstall = $this-&gt;l(&#8216;Are you sure you want to uninstall?&#8217;);<\/p>\n<p><code><\/code><\/p>\n<p>if (!Configuration::get(&#8216;MYMODULE_NAME&#8217;))<\/p>\n<p><code><\/code><\/p>\n<p>$this-&gt;warning = $this-&gt;l(&#8216;No name provided&#8217;);<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code><\/code><br \/>$this-&gt;name: Serves as an internal identifier. The value must be the name of the module&#8217;s folder. Do not use special characters or spaces, and keep it lower-case<\/p>\n<p>$this-&gt;tab: The title for the section that shall contain this module in the back-office modules list.<\/p>\n<p>$this-&gt;version: The version number for the module, displayed in the modules list.<\/p>\n<p>$this-&gt;author: Name of author.<\/p>\n<p>$this-&gt;need_instance: Indicates whether to load the module&#8217;s class when displaying the &#8220;Modules&#8221; page in the back-office. If set at 0, the module will not be loaded, and therefore will spend less resources to generate the &#8220;Modules&#8221; page. If your module needs to display a warning message in the &#8220;Modules&#8221; page, then you must set this attribute to 1.<\/p>\n<p>$this-&gt;ps_versions_compliancy: Indicates which version of prestashop\u00a0this module is compatible with.<\/p>\n<p>parent::__construct(): Call the constructor method from the parent PHP class. By default, this will trigger a lot of actions from PrestaShop. Calling the parent constructor method must be done after the creation of the $this-&gt;name variable and before any use of the $this-&gt;l() translation method.<\/p>\n<p>$this-&gt;displayName: A name for the module, which will be displayed in the back-office&#8217;s modules list.<\/p>\n<p>$this-&gt;description: A description for the module, which will be displayed in the back-office&#8217;s modules list.<\/p>\n<p>$this-&gt;confirmUninstall: A message, asking the administrator if he really does want to uninstall the module.<\/p>\n<p>$this-&gt;warning: Show any warning messages.<br \/>The install() and uninstall() methods<\/p>\n<p>The install() and uninstall() methods make it possible to control what happens when the store administrator installs or uninstalls the module. They must be included in the main class&#8217; block of code (MyModule class) \u2013 at the same level as the constructor method.<\/p>\n<p>The install() method basically does all the things when the module is installed. (For example, registering hooks, updating Configuration values, Adding new tables required for the module, etc.)<br \/><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p>public function install()<\/p>\n<p><code><\/code><\/p>\n<p>{<\/p>\n<p><code><\/code><\/p>\n<p>if (Shop::isFeatureActive()) {<\/p>\n<p><code><\/code><\/p>\n<p>Shop::setContext(Shop::CONTEXT_ALL);<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>if (!parent::install() ||<\/p>\n<p><code><\/code><\/p>\n<p>!Configuration::updateValue(&#8216;CODE_BLOCK_VALUE&#8217;, &#8216;My code block value&#8217;)<\/p>\n<p><code><\/code><\/p>\n<p>) {<\/p>\n<p><code><\/code><\/p>\n<p>return false;<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>return true;<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code><\/code><br \/>The uninstall() method basically does the inverse of install() method. (For example, removing Configuration values which was stored when the module was installed, removing tables, etc.)<br \/><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p>public function uninstall()<\/p>\n<p><code><\/code><\/p>\n<p>{<\/p>\n<p><code><\/code><\/p>\n<p>if (!parent::uninstall() ||<\/p>\n<p><code><\/code><\/p>\n<p>!Configuration::deleteByName(&#8216;CODE_BLOCK_VALUE&#8217;)<\/p>\n<p><code><\/code><\/p>\n<p>) {<\/p>\n<p><code><\/code><\/p>\n<p>return false;<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>return true;<\/p>\n<p><code><\/code><\/p>\n<p>}<\/p>\n<p><code><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code><\/code><br \/>As you can see most of the methods make use of a new Configuration object. This is a PrestaShop-specific object, built to help manage the module settings. It stores these settings in the database without require to use SQL queries. Specifically, this object handles data from the ps_configuration database table.<\/p>\n<p>Now that all basics are in place, reload the back-office&#8217;s &#8220;Modules&#8221; pages, in the &#8220;Front-office features&#8221; section, you should find your module. Install it (or reset it if it is already installed).<\/p>\n<p><a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-442 size-medium\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule-300x98.png\" alt=\"Prestashop Module Development Tutorial\" width=\"300\" height=\"98\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule-300x98.png 300w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule-620x200.png 620w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule.png 981w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>During the module&#8217;s installation, PrestaShop automatically creates a small config.xml file in the module&#8217;s folder, which stores the configuration information. PrestaShop also adds a row to the ps_module SQL table during the module installation.<\/p>\n<p><a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-443 size-medium\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule-300x110.png\" alt=\"Prestashop Module Development\" width=\"300\" height=\"110\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule-300x110.png 300w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule.png 581w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We hope this article about\u00a0<a href=\"http:\/\/www.confianzit.com\/cit-blog\/prestashop-module-development\/\">Prestashop Module Development<\/a> was helpful please post your thoughts and comments \u00a0below .<\/p>\n<p>Confianz Global Inc. is a leading<span>\u00a0<\/span><a href=\"https:\/\/www.confianzit.com\/\" class=\"rank-math-link\">Odoo ERP Development Company in USA<\/a>. Get in touch with us for<span>\u00a0<\/span><a href=\"https:\/\/www.confianzit.com\/openerp-customization\" class=\"rank-math-link\">Odoo ERP Customization<\/a><span>\u00a0<\/span>&amp;<span>\u00a0<\/span><a href=\"https:\/\/www.confianzit.com\/odoo-implementation\" class=\"rank-math-link\">Implementation<\/a>. <a href=\"https:\/\/www.confianzit.com\/contact-us\">Give us a call today<\/a>!<\/p>\n<p><span style=\"color: #444444;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>[\/et_pb_text][\/et_pb_column][et_pb_column type=&#8221;2_5&#8243; _builder_version=&#8221;4.16&#8243; custom_padding=&#8221;|||&#8221; global_colors_info=&#8221;{}&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_code _builder_version=&#8221;4.21.0&#8243; _module_preset=&#8221;default&#8221; animation_style=&#8221;bounce&#8221; animation_direction=&#8221;top&#8221; sticky_position=&#8221;top&#8221; sticky_offset_top=&#8221;20px&#8221; sticky_limit_bottom=&#8221;column&#8221; sticky_transition=&#8221;off&#8221; motion_trigger_start=&#8221;top&#8221; global_module=&#8221;2151&#8243; locked=&#8221;off&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<div class=\"blog-floating-form\"><!-- [et_pb_line_break_holder] -->  <\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_62 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.confianzit.com\/cit-blog\/prestashop-module-development\/#Talk_to_our_experts_now\" title=\"    Talk to our experts now  \">    Talk to our experts now  <\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.confianzit.com\/cit-blog\/prestashop-module-development\/#Talk_To_Our_Experts_Now\" title=\"Talk To Our Experts Now\n\t\">Talk To Our Experts Now\n\t<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"Talk_to_our_experts_now\"><\/span><!-- [et_pb_line_break_holder] -->    Talk to our experts now<!-- [et_pb_line_break_holder] -->  <span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><!-- [et_pb_line_break_holder] -->  \n<div class=\"wpcf7 no-js\" id=\"wpcf7-f1888-o1\" lang=\"en-US\" dir=\"ltr\">\n<div class=\"screen-reader-response\"><p role=\"status\" aria-live=\"polite\" aria-atomic=\"true\"><\/p> <ul><\/ul><\/div>\n<form action=\"\/cit-blog\/wp-json\/wp\/v2\/posts\/441#wpcf7-f1888-o1\" method=\"post\" class=\"wpcf7-form init\" aria-label=\"Contact form\" novalidate=\"novalidate\" data-status=\"init\">\n<div style=\"display: none;\">\n<input type=\"hidden\" name=\"_wpcf7\" value=\"1888\" \/>\n<input type=\"hidden\" name=\"_wpcf7_version\" value=\"5.8.6\" \/>\n<input type=\"hidden\" name=\"_wpcf7_locale\" value=\"en_US\" \/>\n<input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f1888-o1\" \/>\n<input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" \/>\n<input type=\"hidden\" name=\"_wpcf7_posted_data_hash\" value=\"\" \/>\n<input type=\"hidden\" name=\"_wpcf7_recaptcha_response\" value=\"\" \/>\n<\/div>\n<div class=\"form-block\" style=\"    background: #fff;\">\n\t<h3 style=\"    background: #0C2464;\n    border-bottom: 5px solid #cecece;\n    border-radius: 5px 5px 90px 90px;\n    margin: 0 auto;\n    text-align: center;\n    padding: 20px;\n    color: #fff;    margin-bottom: 15px;\"><span class=\"ez-toc-section\" id=\"Talk_To_Our_Experts_Now\"><\/span><b>Talk To Our Experts Now<\/b>\n\t<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\t<div style=\"padding:20px;\">\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-name\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required your-name\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Name\" value=\"\" type=\"text\" name=\"your-name\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-email\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email your-email\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Email\" value=\"\" type=\"email\" name=\"your-email\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-number\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-tel wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-tel your-number\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Phone Number\" value=\"\" type=\"tel\" name=\"your-number\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"message\"><textarea cols=\"40\" rows=\"10\" class=\"wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required form-message\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Message\" name=\"message\"><\/textarea><\/span>\n\t\t<\/p>\n\t<span class=\"wpcf7-form-control-wrap recaptcha\" data-name=\"recaptcha\"><span data-sitekey=\"6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" class=\"wpcf7-form-control wpcf7-recaptcha g-recaptcha\"><\/span>\r\n<noscript>\r\n\t<div class=\"grecaptcha-noscript\">\r\n\t\t<iframe loading=\"lazy\" src=\"https:\/\/www.google.com\/recaptcha\/api\/fallback?k=6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" frameborder=\"0\" scrolling=\"no\" width=\"310\" height=\"430\">\r\n\t\t<\/iframe>\r\n\t\t<textarea name=\"g-recaptcha-response\" rows=\"3\" cols=\"40\" placeholder=\"reCaptcha Response Here\">\r\n\t\t<\/textarea>\r\n\t<\/div>\r\n<\/noscript>\r\n<\/span>\n\t\t<div class=\"form-buttons\">\n\t\t\t<p><input class=\"wpcf7-form-control wpcf7-submit has-spinner\" type=\"submit\" value=\"Get a free quote\" \/>\n\t\t\t<\/p>\n\t\t<\/div>\n\t<\/div>\n<\/div><div class=\"wpcf7-response-output\" aria-hidden=\"true\"><\/div>\n<\/form>\n<\/div>\n<!-- [et_pb_line_break_holder] --><\/div>\n<p>[\/et_pb_code][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explains how to create a module for prestashop. Let\u2019s create a simple module and will name it \u2018 My module \u2019.First, create a folder for the module in the &lt;HOME_DIRECTORY&gt;\/modules folder and it should have the same name as the module, with no space, only alphanumerical characters, the hyphen and the underscore, all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development.jpg\"><img class=\"alignnone size-medium wp-image-444\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/Prestashop-Module-Development-300x300.jpg\" alt=\"Prestashop Module Development\" width=\"300\" height=\"300\"><\/a>\n\nThis article explains how to create a module for <strong>prestashop<\/strong>. Let\u2019s create a simple module and will name it \u2018 <strong>My module<\/strong> \u2019.\nFirst, create a folder for the module in the &lt;HOME_DIRECTORY&gt;\/modules folder and it should have the same name as the module, with no space, only alphanumerical characters, the hyphen and the underscore, all in lowercase:\n(This will be our folder structure: &lt;HOME_DIRECTORY&gt;\/modules\/mymodule.)\n\nThis folder must contain the main file, a PHP file of the same name as the folder, which will handle most of the processing: mymodule.php\n<!--more-->\nThe main mymodule.php file must start with the following test:\n<code>\n&lt;?php\nif (!defined('_PS_VERSION_'))\nexit;\n<\/code>\n\nThis checks for the existence of an always-existing PrestaShop constant (its version number), and if it does not exist, it stops the module from loading. The sole purpose of this is to prevent malicious visitors to load this file directly.\n\nThe main file (mymodule.php) must contain the module's main class, and that class must bear the same name as the module and its folder, in CamelCase and that class must extend the Module class, in order to inherit all its methods and attributes. (It can just as well extend any class derived from Module, for specific needs: PaymentModule, ModuleGridEngine, ModuleGraph, etc.)\n\nAt this stage, the module can already be seen in the \"Modules\" page in the back-office (in the \"Other modules\" section) \u2013 albeit with no real name nor thumbnail.\n\nNow, let's fill the class code block with the essential constructor lines. Our mymodule.php file will look like this now. The use of each attribute in the constructor is explained below.\n<code>\n&lt;?php\nif (!defined('_PS_VERSION_'))\nexit;\nclass MyModule extends Module\n{\npublic function __construct()\n{\n$this-&gt;name = 'mymodule';\n$this-&gt;tab = 'front_office_features';\n$this-&gt;version = '1.0';\n$this-&gt;author = 'Firstname Lastname';\n$this-&gt;need_instance = 0;\n$this-&gt;ps_versions_compliancy = array('min' =&gt; '1.5', 'max' =&gt; '1.6');\nparent::__construct();\n$this-&gt;displayName = $this-&gt;l('My module');\n$this-&gt;description = $this-&gt;l('Description of my module.');\n$this-&gt;confirmUninstall = $this-&gt;l('Are you sure you want to uninstall?');\nif (!Configuration::get('MYMODULE_NAME'))\n$this-&gt;warning = $this-&gt;l('No name provided');\n}\n}\n<\/code>\n$this-&gt;name: Serves as an internal identifier. The value must be the name of the module's folder. Do not use special characters or spaces, and keep it lower-case\n\n$this-&gt;tab: The title for the section that shall contain this module in the back-office modules list.\n\n$this-&gt;version: The version number for the module, displayed in the modules list.\n\n$this-&gt;author: Name of author.\n\n$this-&gt;need_instance: Indicates whether to load the module's class when displaying the \"Modules\" page in the back-office. If set at 0, the module will not be loaded, and therefore will spend less resources to generate the \"Modules\" page. If your module needs to display a warning message in the \"Modules\" page, then you must set this attribute to 1.\n\n$this-&gt;ps_versions_compliancy: Indicates which version of <a href=\"http:\/\/www.confianzit.com\/products\/opensource-products\/openerp-prestashop-connector\">PrestaShop<\/a> this module is compatible with.\n\nparent::__construct(): Call the constructor method from the parent PHP class. By default, this will trigger a lot of actions from PrestaShop. Calling the parent constructor method must be done after the creation of the $this-&gt;name variable and before any use of the $this-&gt;l() translation method.\n\n$this-&gt;displayName: A name for the module, which will be displayed in the back-office's modules list.\n\n$this-&gt;description: A description for the module, which will be displayed in the back-office's modules list.\n\n$this-&gt;confirmUninstall: A message, asking the administrator if he really does want to uninstall the module.\n\n$this-&gt;warning: Show any warning messages.\nThe install() and uninstall() methods\n\nThe install() and uninstall() methods make it possible to control what happens when the store administrator installs or uninstalls the module. They must be included in the main class' block of code (MyModule class) \u2013 at the same level as the constructor method.\n\nThe install() method basically does all the things when the module is installed. (For example, registering hooks, updating Configuration values, Adding new tables required for the module, etc.)\n<code>\npublic function install()\n{\nif (Shop::isFeatureActive()) {\nShop::setContext(Shop::CONTEXT_ALL);\n}\nif (!parent::install() ||\n!Configuration::updateValue('CODE_BLOCK_VALUE', 'My code block value')\n) {\nreturn false;\n}\nreturn true;\n}\n<\/code>\nThe uninstall() method basically does the inverse of install() method. (For example, removing Configuration values which was stored when the module was installed, removing tables, etc.)\n<code>\npublic function uninstall()\n{\nif (!parent::uninstall() ||\n!Configuration::deleteByName('CODE_BLOCK_VALUE')\n) {\nreturn false;\n}\nreturn true;\n}\n<\/code>\nAs you can see most of the methods make use of a new Configuration object. This is a PrestaShop-specific object, built to help manage the module settings. It stores these settings in the database without require to use SQL queries. Specifically, this object handles data from the ps_configuration database table.\n\nNow that all basics are in place, reload the back-office's \"Modules\" pages, in the \"Front-office features\" section, you should find your module. Install it (or reset it if it is already installed).\n\n<a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule.png\"><img class=\"alignnone wp-image-442 size-medium\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/InstalledModule-300x98.png\" alt=\"Prestashop Module Development Tutorial\" width=\"300\" height=\"98\"><\/a>\n\nDuring the module's installation, PrestaShop automatically creates a small config.xml file in the module's folder, which stores the configuration information. PrestaShop also adds a row to the ps_module SQL table during the module installation.\n\n<a href=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule.png\"><img class=\"alignnone wp-image-443 size-medium\" src=\"http:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2014\/07\/PsModule-300x110.png\" alt=\"Prestashop Module Development\" width=\"300\" height=\"110\"><\/a>\n\n&nbsp;\n\nWe hope this article about&nbsp;<a href=\"http:\/\/www.confianzit.com\/cit-blog\/prestashop-module-development\/\">Prestashop Module Development<\/a> was helpful please post your thoughts and comments &nbsp;below .visit our&nbsp;<span style=\"color: #444444;\"><a href=\"http:\/\/www.confianzit.com\/products\/opensource-products\/openerp-prestashop-connector\">Prestashop connector<\/a>&nbsp;<\/span>\n\n&nbsp;","_et_gb_content_width":"","footnotes":""},"categories":[48],"tags":[],"_links":{"self":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/441"}],"collection":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/comments?post=441"}],"version-history":[{"count":7,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":29096,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/441\/revisions\/29096"}],"wp:attachment":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/media?parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/categories?post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/tags?post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}