Essential Prestashop Functions – Day 1

Prestashop has lots of time-saving functions that we can use when developing modules or extensions. Let’s have a look at the first batch of Prestashop Functions, and in which context they might come in use.

If you want to dive into Prestashop development, these are likely the first methods you’ll be needing.

NOTICE: Values with “=” in the declaration are optionals.

Managing Configuration values in Prestashop

[php]

// Updating a single/multiple value/s
Configuration::updateValue($key, $values, $html = false, $id_shop_group = null, $id_shop = null);

// Getting a single value
Configuration::get($key, $id_lang = null, $id_shop_group = null, $id_shop = null)

// Getting Multiple Values
Configuration::getMultiple($keys, $id_lang = null, $id_shop_group = null, $id_shop = null)

// Getting the same key in all languages
Configuration::getInt($key, $id_shop_group = null, $id_shop = null)

// Erase the given entry
Configuration::deleteByName($key)

[/php]

These methods are used to interact with the *prefix_*configuration table. It’s the quickest way to store simple data in Prestashop.

Example Usage

[php]
// Saving a normal string
$my_text = ‘This is some text’;
Configuration::updateValue(‘MY_CONFIG_PARAMETER’, $my_text);

// Saving an html string
$my_text = ‘<p>This is some text</p>’;
Configuration::updateValue(‘MY_CONFIG_PARAMETER’, $my_text, true);

// Saving multiple values, KEYS represend ID_LANG
$my_texts = array(
1 => ‘This is some text’,
2 => ‘This is some text in another language’
);
Configuration::updateValue(‘MY_CONFIG_PARAMETER_MULTILANG’, $my_text);

// Getting a single value
$my_text = Configuration::get(‘MY_CONFIG_PARAMETER’);

// Getting a single value for the current language only
$my_text = Configuration::get(‘MY_CONFIG_PARAMETER’, $this->context->language->id);

// Getting Multiple Values
$keys_to_get = array(‘MY_CONFIG_PARAMETER’, ‘OTHER_CONFIG_PARAMETER’);
Configuration::getMultiple($keys_to_get);

// Delete the given key
Configuration::deleteByName(‘MY_CONFIG_PARAMETER’);
[/php]

Getting POST and GET in Prestashop

[php]
Tools::getValue($key, $default_value = false)

[/php]

Retrieves the current POST or GET variable with the given key

Example Usage

[php]
// Getting either POST or GET with the key ‘myvalue’, the second parameter is what to assign then nothing is found.
$value = Tools::getValue(‘myvalue’, 4);
// If no ‘myvalue’ POST or GET variable is set, $value will be 4.
[/php]

Checking for submitted data in Prestashop

[php]
Tools::isSubmit($key);
[/php]

Check whether or not the given key has been submitted to the current page.

Example Usage

[php]
if(Tools::isSubmit(‘myvalue’))
{
// do something
}
[/php]

Displaying Errors in Prestashop

[php]
Tools::displayError($string = ‘Fatal error’, $htmlentities = true, Context $context = null)
[/php]

Displays an error with proper formatting.

Example Usage

[php]
if($myvalue != 1)
{
// will need to be displayed later on
$error_string = Tools::displayError(‘It is different from 1’);
}
[/php]

Displaying Confirmations in Prestashop Modules

[php]
$this->displayConfirmation($string);
[/php]

Displays a properly formatted confirmation message. $this refers to a module, it is therefore only available in module contexts only. Refer to the proper variable elsewhere.

[php]

// this is inside a module’s method
if($myvalue == 1)
{
// will need to be displayed later on
$conf_string = $this->displayConfirmation(‘Updated!’);
}

[/php]

Adding CSS in Prestashop

[php]
ControllerCore::addCSS($css_uri, $css_media_type = ‘all’, $offset = null, $check_path = true)
[/php]

Appends the given CSS file/s to the page

Example Usage

[php]

// this is inside a module’s method
$this->context->controller->addCSS($this->_path.’css/myfile.css’, ‘all’);

// this is inside a controller’s method
$this->addCSS(_THEME_CSS_DIR_.’product.css’);

// adding multiple files
$files = array(
_THEME_CSS_DIR_.’product.css’ => ‘all’,
_THEME_CSS_DIR_.’other_css.css’ => ‘all’
);
$this->addCSS($files);

[/php]

Adding Javascript in Prestashop

[php]
ControllerCore::addJS($js_uri, $check_path = true);
[/php]

Appends the given JS file/s to the page

Example Usage

[php]

// this is inside a module’s method
$this->context->controller->addCSS($this->_path.’css/myfile.css’, ‘all’);

// this is inside a controller’s method
$this->addCSS(_THEME_CSS_DIR_.’product.css’);

[/php]

Adding jQuery Plugins in Prestashop

[php]
ControllerCore::addJqueryPlugin($name, $folder = null, $css = true)
[/php]

Appends the given jQuery Plugin/s to the page

Example Usage

[php]

// this is inside a module’s method, multiple files
$this->context->controller->addjqueryPlugin(‘fancybox’);

// this is inside a controller’s method
$this->addjqueryPlugin(‘fancybox’);

// this is inside a controller’s method, multiple values
$this->addjQueryPlugin(array(‘scrollTo’, ‘alerts’, ‘chosen’, ‘autosize’, ‘fancybox’ ));

// this is inside a controller’s method, without loading the plugin’s CSS
$this->addjQueryPlugin(‘growl’, null, false);

[/php]

Adding jQuery UI Components in Prestashop

[php]
ControllerCore::addJqueryUI($component, $theme = ‘base’, $check_dependencies = true);
[/php]

Appends the given jQuery UI component/s to the page

Example Usage

[php]

// this is inside a module’s method, single file
$this->context->controller->addjQueryUI(ui.datepicker’);

// this is inside a controller’s method, multiple files
$this->addJqueryUI(array(‘ui.slider’, ‘ui.datepicker’));

[/php]

Wrapping it up

We saw quite a number of useful PrestaShop Functions today, which are enough to start coding your own, simple module. Apart from trying to remember them all, I suggest creating code snippets with some text expander, so you can quickly drop them in, without having to remember the exact syntax.

Looking for quality PrestaShop hosting? Check out Arvixe Web Solutions.

Tags: , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *