...
This function builds upon the previous function by checking whether data was posted, validating the data, and then displaying an error or success message after the module’s name and before the form. The displayErrors()
function groups all the error messages into a single box. The displayConf()
function displays a confirmation message in the new PrestaShop v1.4 style. It is the first if statement in the getContent()
function that checks whether data was posted. The first line in the if statement gets the value of the text field and second line gets the value of the radio buttons. The radio button value is converted to an integer for data validation purposes. If the radio button value isn’t either 0 or 1, then an error message is displayed. This ensures that the setting always has a valid value. If the radio button has a valid value, then the text field and radio button values are written to the database, then a success message is displayed. Your module will now save the configuration settings.
...
Code Block | ||
---|---|---|
| ||
public function hookLeftColumn($params) { global $smarty; $smarty->assign(array( 'name' => Configuration::get('HELLO_WORLD_NC_NAME'), 'showName' => (int)(Configuration::get('HELLO_WORLD_NC_SHOW_NAME')) )); return $this->display(__FILE__, 'helloworldnc.tpl'); } public function hookRightColumn($params) { return $this->hookLeftColumn($params); } public function hookHeader() { Tools::addJS($this->_path.'js/helloworldnc.js'); Tools::addCSS($this->_path.'css/helloworldnc.css', 'all'); } |
The hookLeftColumn
function defines what code should be added when the module is placed in the left column hook. The first line in the function makes the global $smarty
variable accessible to the function. This variable can be used to pass variables into a template file. The next line gets the text field and radio settings from the database and creates Smarty variables. The last line of the function calls the helloworldnc.tpl
file in the module’s directory, which has access to the Smarty variables that were created. It then returns the HTML generated by the template, which is displayed in place of the {$HOOK_LEFT_COLUMN}
along with the HTML from any other modules that are also in the hook.
The hookRightColumn
function simply calls the hookLeftColumn
function, passing its parameters and returning the result. This is a shortcut that makes it so that the module will display the exact same in either the left or right column. Doing this also makes the code easier to maintain, since you don’t have to worry about changing code in two places.
The hookHeader
function is used to add Javascript and CSS code inside the <head>
tag of the website. Be careful how you use it, since it is easy to confuse it with the hookTop
function, which adds code to the top of the website in the header. The first line in the function adds /modules/helloworldnc/js/examplenc.js
to the list of Javascript files and the second line adds /modules/helloworldnc/css/helloworldnc.css
to the list of CSS files with media type all. This will add the following code in the <head>
tag of the website (if CCC is disabled):
...
Smarty templates are files ending in .tpl
that contain HTML code and embedded PHP code using a special Smarty syntax. See the official Smarty documentation for more information. Here’s an example helloworldnc.tpl
:
Code Block | ||
---|---|---|
| ||
<div id="hello_world_block_left" class="block"> <h4>{l s='Message' mod='helloworldnc'}</h4> <div class="block_content"> <p>Hello, {if isset($showName) AND $showName AND isset($name) AND $name}{$name}{else}World{/if}</p> </div> </div> |
...
The second line adds the label "Message" at the top of the block and makes it translatable in the Back Office. The function name l
stands for language, the parameter s
stands for string and the parameter mod
stands for module. It is important to include the mod
parameter with the name of the module for the translations to appear in the module translations.
...
The fourth line adds the message in the content area. When the module has its default configuration, the message will be "Hello, World". If the name is changed on the configuration page, it will also change in the message. If the module has been configured to hide the name, then only "Hello" will be displayed.
You now have a fully functioning module.
...
Although you have a fully functioning module, it doesn’t have any CSS or JS, which is usually required to create any module that is useful. In the
Adding the Hooks section, you added code to include JavaScript and CSS files in the module, but you didn’t actually put anything inside these files.
It’s a good idea to put the CSS in a separate /css
directory and the JavaScript in a separate /js
directory to keep a clean file structure that matches the structure PrestaShop uses. Here’s an example helloworldnc.css
:
Code Block | ||
---|---|---|
| ||
div#hello_world_block_left p { font-weight: bold } |
...
Code Block | ||
---|---|---|
| ||
div#hello_world_block_left p { font-weight: bold; color: green } |
PrestaShop 1.4.3 uses jQuery v1.4.4, so including it in your module is unnecessary. The only JavaScript you need to include is any third-party libraries you require that build on jQuery. If you need to include inline JavaScript to the module's TPL file, be aware that Smarty will generate an error if there are any curly braces in the code. To prevent the errors, you should add {literal}
before the JavaScript code and {/literal}
after like this:
Code Block | ||
---|---|---|
| ||
{literal} function testFunction() { alert('Test'); } {/literal} |
If you need to combine both JavaScript and Smarty tags, then it's better to replace the JavaScript curly braces with the {ldelim}
and {rdelim}
tags like this:
Code Block | ||
---|---|---|
| ||
function testFunction() {ldelim} alert('{$message}'); {rdelim} |
This code will display the contents of the $message
Smarty variable in a JavaScript alert box when the testFunction()
is called.
Creating a Back Office Tab
The first step to creating a new Back Office tab is coming up with a unique filename ending in .php
. It is convention for a tab’s filename to start with Admin. One way to ensure your tab name is unique is to use a vendor suffix. For example, Nethercott Constructions uses the NC suffix on its tabs to ensure they are unique.
...
The class name must match the filename of the tab. The name of the class can be anything, though it is convention for the class name to start with Admin
.
The next step is to add the constructor inside the class. Here’s an example constructor:
...
The first line creates a private variable that contains the Hello World module object. The $this->className
line specifies the class and the $this->table
line specifies which database table is used to store and display data. This can be used to display a table of categories, products or other data from the database. Since this tab is simply a configuration page, the class name is 'Configuration' and the table is 'configuration'.
The $this->_helloWorld
line creates the instance of the Hello World module used later in the tab to display the configuration page. The parent::__construct()
line calls the AdminTab
class constructor, which sets up tab’s ID and default error messages.
Lastly, the display()
and postProcess()
functions need to be added. For example:
...
To avoid duplicating code, the display()
function calls the displayForm()
function from the Hello World module object to display the same configuration page. The postProcess()
function calls the getContent()
function of the Hello World module object to save the configuration page settings.
...
The customer cookie is read on line 94 (in PrestaShop v1.4.2) of init.php
and the employee cookie is read on line 32 of /admin/init.php
. To access the cookie from inside PrestaShop, add global $cookie;
(or add $cookie
to the list of global variables) to the top of the function in a class or at the top of a non-class file. A variable in the cookie can then be accessed or changed using $cookie->variable
. To access the cookie from outside of PrestaShop, use code like the following:
...
There are also variables for product customisation. For example, pictures_1
contains the filenames of the images the customer has uploaded to product 1 (in the upload directory) and textfields_1
contains the text the customer has uploaded to product 1. Use the following code to get the customisation files and textfields of product 1:
...
There are also pagination and filter variables stored in the employee cookie so that the state of the tables is saved. For example, the order_pagination
variable stores how many orders are displayed per page and orderFilter_id_order
stores the filter applied to the id_order
column of the orders table.
...
Here are descriptions of the database tables in PrestaShop v1.4.2. By default, each table has a prefix of ps_
. When you install PrestaShop, you have the option of changing this prefix, which lets you install PrestaShop multiple times in the same database, and also helps to prevent table name conflicts with other shopping carts installed in the same database. The table names are all lowercase and use underscores between words, and include the types of objects in the table. For example, the ps_product
table contains all the product information. Tables that end with _lang
contain translations for the object. For example, ps_product_lang
contains all the product translations. When a table links together two types of objects, both objects are included in the table name. For example, ps_category_product
links products to categories. The ID of each table starts with id_
followed by the object name. For example, the ID of the ps_product
table is id_product
.
Table Name | Description | Primary Key |
---|---|---|
ps_access | The employee profile permissions. | id_profile, id_tab |
ps_accessory | Product accessories. | id_product_1, id_product_2 |
ps_address | Customer, manufacturer and supplier addresses. | id_address |
ps_address_format | Address format for each country. | id_country |
ps_alias | Search keyword alias | id_alias |
ps_attachment | Product attachments. | id_attachment |
ps_attachment_lang | Product attachment names and descriptions. | id_attachment, id_lang |
ps_attribute | Product attributes. | id_attribute |
ps_attribute_group | Product attribute groups. | id_attribute_group |
ps_attribute_group_lang | Product attribute group names. | id_attribute_group, id_lang |
ps_attribute_impact | Product attribute price and weight impacts. | id_attribute_impact |
ps_attribute_lang | Product attribute names. | id_attribute, id_lang |
ps_carrier | Carriers. | id_carrier |
ps_carrier_group | Carrier customer group restrictions. | id_carrier, id_group |
ps_carrier_lang | Carrier transit time messages. | id_carrier |
ps_carrier_zone | Zones each carrier is available | id_carrier, id_zone |
ps_cart | Customer and guest carts. | id_cart |
ps_cart_discount | Voucher codes used in carts. | id_cart, id_discount |
ps_cart_product | Products in customer carts. | id_cart, id_product, id_product_attribute |
ps_category | Product categories. | id_category |
ps_category_group | Product category customer group restrictions. | id_category, id_group |
ps_category_lang | Product category names and descriptions. | id_category, id_lang |
ps_category_product | Products in each category. | id_category, id_product |
ps_cms | CMS pages. | id_cms |
ps_cms_block | CMS blocks. | id_cms_block |
ps_cms_block_lang | CMS block names. | id_cms_block, id_lang |
ps_cms_block_page | CMS block pages. | id_cms_block_page |
ps_cms_category | CMS categories. | id_cms_category |
ps_cms_category_lang | CMS category names. | id_cms_category, id_lang |
ps_cms_lang | CMS page content. | id_cms, id_lang |
ps_configuration | Configuration settings. | id_configuration |
ps_configuration_lang | Configuration setting translations. | id_configuration, id_lang |
ps_connections | Visitor IP addresses, referrers and pages visited. | id_connections |
ps_connections_page | Start and end times of page visits. | id_connections, id_page, time_start |
ps_connections_source | Visitor referrers and search engine keywords. | id_connections_source |
ps_contact | Contact form subjects. | id_contact |
ps_contact_lang | Contact form subject names. | id_contact, id_lang |
ps_country | Countries. | id_country |
ps_country_lang | Country names. | id_country, id_lang |
ps_county | Counties | id_county |
ps_county_zip_code | County zip codes. | id_county, from_zip_code, to_zip_code |
ps_currency | Currencies. | id_currency |
ps_customer | Customers. | id_customer |
ps_customer_group | Customer groups. | id_customer, id_group |
ps_customer_message | Customer messages through contact form. | id_customer_message |
ps_customer_thread | Threads of messages through contact form. | id_customer_thread |
ps_customization | Customised product data in cart. | id_customization, id_cart, id_product |
ps_customization_field | Product customisation fields. | id_customization_field |
ps_customization_field_lang | Product customisation field names. | id_customization_field, id_lang |
ps_customized_data | Customised product data entered by customers. | id_customization, type, index |
ps_date_range | Page view date ranges. | id_date_range |
ps_delivery | Carrier delivery prices. | id_delivery |
ps_discount | Vouchers. | id_discount |
ps_discount_category | Voucher category restrictions. | id_category, id_discount |
ps_discount_lang | Voucher descriptions. | id_discount, id_lang |
ps_discount_type | Voucher types. | id_discount_type |
ps_discount_type_lang | Voucher type names. | id_discount_type, id_lang |
ps_editorial | Homepage logo links. | id_editorial |
ps_editorial_lang | Homepage text. | id_editorial, id_lang |
ps_employee | Back Office employees. | id_employee |
ps_feature | Product features. | id_feature |
ps_feature_lang | Product feature names. | id_feature, id_lang |
ps_feature_product | Links feature values to products. | id_feature, id_product |
ps_feature_value | Product feature values. | id_feature_value |
ps_feature_value_lang | Product feature value names. | id_feature_value, id_lang |
ps_group | Customer groups and reductions. | id_group |
ps_group_lang | Customer group names. | id_group, id_lang |
ps_group_reduction | Customer group category reductions. | id_group_reduction |
ps_guest | Guests. | id_guest |
ps_help_access | Viewed help articles and versions. | id_help_access |
ps_hook | Module hook names and descriptions. | id_hook |
ps_hook_module | Position of modules in each hook. | id_module, id_hook |
ps_hook_module_exceptions | Page exceptions for each module. | id_hook_module_exceptions |
ps_image | Product images. | id_image |
ps_image_lang | Product image legends. | id_image, id_lang |
ps_image_type | Product image thumbnail sizes. | id_image_type |
ps_import_match | Saved CSV import configurations. | id_import_match |
ps_lang | Languages. | id_lang |
ps_log | Log of PHP errors and warnings. | id_log |
ps_manufacturer | Manufacturers. | id_manufacturer |
ps_manufacturer_lang | Manufacturer descriptions. | id_manufacturer, id_lang |
ps_memcached_servers | Memcached servers. | id_memcached_server |
ps_message | Order messages. | id_message |
ps_message_readed | Order messages that have been marked as read. | id_message, id_employee |
ps_meta | Page names. | id_meta |
ps_meta_lang | Page meta information. | id_meta, id_lang |
ps_module | Modules directories. | id_module |
ps_module_country | Module country restrictions. | id_module, id_country |
ps_module_currency | Module currency restrictions. | id_module, id_currency |
ps_module_group | Module customer group restrictions. | id_module, id_group |
ps_operating_system | Recognised operating systems. | id_operating_system |
ps_orders | Orders. | id_order |
ps_order_detail | Order products. | id_order_detail |
ps_order_discount | Order vouchers. | id_order_discount |
ps_order_history | Order status history. | id_order_history |
ps_order_message | Predefined order messages. | id_order_message |
ps_order_message_lang | Predefined order message names. | id_order_message, id_lang |
ps_order_return | Merchandise returns. | id_order_return |
ps_order_return_detail | Merchandise return products. | id_order_return, id_order_detail, id_customization |
ps_order_return_state | Merchandise return statuses. | id_order_return_state |
ps_order_return_state_lang | Merchandise return status names. | id_order_return_state, id_lang |
ps_order_slip | Credit slips | id_order_slip |
ps_order_slip_detail | Credit slip products. | id_order_slip, id_order_detail |
ps_order_state | Order statuses. | id_order_state |
ps_order_state_lang | Order status names. | id_order_state, id_lang |
ps_pack | Product packs. | id_product_pack, id_product_item |
ps_page | Pages. | id_page |
ps_pagenotfound | Page not found data. | id_pagenotfound |
ps_page_type | Page names. | id_page_type |
ps_page_viewed | Viewed pages. | id_page, id_date_range |
ps_payment_cc | Credit card information. | id_payment_cc |
ps_product | Product information. | id_product |
ps_product_attribute | Product attribute information. | id_product_attribute |
ps_product_attribute_combination | Product attribute combinations. | id_attribute, id_product_attribute |
ps_product_attribute_image | Product attribute images. | id_product_attribute, id_image |
ps_product_country_tax | Product country taxes. | id_product, id_country, id_tax |
ps_product_download | Downloadable products. | id_product_download |
ps_product_group_reduction_cache | Product customer group reductions. | id_product, id_group |
ps_product_lang | Product names and descriptions. | id_product, id_lang |
ps_product_sale | Product sale data. | id_product |
ps_product_tag | Product tags. | id_product, id_tag |
ps_profile | Employee profile. | id_profile |
ps_profile_lang | Employee profile name. | id_profile, id_lang |
ps_quick_access | Back Office quick accesses. | id_quick_access |
ps_quick_access_lang | Back Office quick access names. | id_quick_access, id_lang |
ps_range_price | Carrier price ranges. | id_range_price |
ps_range_weight | Carrier weight ranges. | id_range_weight |
ps_referrer | Affiliate program accounts. | id_referrer |
ps_referrer_cache | Affiliate program referrals. | id_connections_source, id_referrer |
ps_required_field | Required fields. | id_required_field |
ps_scene | Category page product banners. | id_scene |
ps_scene_category | Links product banners to categories. | id_scene, id_category |
ps_scene_lang | Category page product banner names. | id_scene, id_lang |
ps_scene_products | Category page banner products. | id_scene, id_product, x_axis, y_axis |
ps_search_engine | Recognised search engines. | id_search_engine |
ps_search_index | PrestaShop search engine keyword index. | id_product, id_word |
ps_search_word | PrestaShop search engine keywords. | id_word |
ps_sekeyword | External search engine keywords. | id_sekeyword |
ps_specific_price | Specific prices. | id_specific_price |
ps_specific_price_priority | Specific price priorities. | id_specific_price_priority, id_product |
ps_state | States. | id_state |
ps_statssearch | PrestaShop search engine statistics. | id_statssearch |
ps_stock_mvt | Stock movements. | id_stock_mvt |
ps_stock_mvt_reason | Stock movement reasons. | id_stock_mvt_reason |
ps_stock_mvt_reason_lang | Stock movement reason names. | id_stock_mvt_reason, id_lang |
ps_store | Store addresses. | id_store |
ps_subdomain | Cookie subdomains. | id_subdomain |
ps_supplier | Suppliers. | id_supplier |
ps_supplier_lang | Supplier names. | id_supplier, id_lang |
ps_tab | Back Office tabs. | id_tab |
ps_tab_lang | Back Office tab names. | id_tab, id_lang |
ps_tag | Product tags used in tag cloud. | id_tag |
ps_tax | Taxes. | id_tax |
ps_tax_lang | Tax names. | id_tax, id_lang |
ps_tax_rule | Tax rules. | id_tax_rule |
ps_tax_rules_group | Tax rule names. | id_tax_rules_group |
ps_timezone | Timezones. | id_timezone |
ps_webservice_account | Webservice accounts. | id_webservice_account |
ps_webservice_permission | Webservice permissions. | id_webservice_permission |
ps_web_browser | Recognised web browsers | id_web_browser |
ps_zone | Carrier zones. | id_zone |