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


// 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)

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

Example Usage

// 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');

Getting POST and GET in Prestashop

Tools::getValue($key, $default_value = false)

Retrieves the current POST or GET variable with the given key

Example Usage

// 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.

Checking for submitted data in Prestashop

Tools::isSubmit($key);

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

Example Usage

if(Tools::isSubmit('myvalue'))
{
	// do something
}

Displaying Errors in Prestashop

Tools::displayError($string = 'Fatal error', $htmlentities = true, Context $context = null)

Displays an error with proper formatting.

Example Usage

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

Displaying Confirmations in Prestashop Modules

$this->displayConfirmation($string);

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.


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

Adding CSS in Prestashop

ControllerCore::addCSS($css_uri, $css_media_type = 'all', $offset = null, $check_path = true)

Appends the given CSS file/s to the page

Example Usage


// 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);

Adding Javascript in Prestashop

ControllerCore::addJS($js_uri, $check_path = true);

Appends the given JS file/s to the page

Example Usage


// 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 jQuery Plugins in Prestashop

ControllerCore::addJqueryPlugin($name, $folder = null, $css = true)

Appends the given jQuery Plugin/s to the page

Example Usage


// 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);

Adding jQuery UI Components in Prestashop

ControllerCore::addJqueryUI($component, $theme = 'base', $check_dependencies = true);

Appends the given jQuery UI component/s to the page

Example Usage


// 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'));

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.

Additional Resources

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • http://www.pddesign.com.au/ Paul D

    Thanks again Nemo, love your contributions to the Prestashop community. Keep up the hard work!

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: