Use of Context Object in Prestashop

What is the Context object?

The Context is a new technical feature of PrestaShop 1.5 and 1.6.
There are two types of goals.
Stop using global variables.
To change the context of some methods.
The Context is a registry for PHP variables that were previously accessed as global.
This is a light implementation of Registry pattern, a classes that data are stored like customer, cookie, cart, currency, language, theme, Smarty, etc.
PrestaShop 1.5 and 1.6 the context are available in more cleanly to PrestaShop 1.4.
For example :
PrestaShop 1.4 – $cookie->id_lang;
PrestaShop 1.5 and 1.6 – $this->context->language->id;

What is stored by the Context?
These are the full list of back-end context objects:
Language: Set the customer or employee language.
Country: Default country.
Currency: Set the customer currency or the shop’s default currency.
Shop: Current shop.
Cookie: Cookie instance.
Link: Link instance.
Smarty: Smarty instance.
Customer Context access only below objects:
1. Customer: Existing customer retrieved from the cookie or default customer
2. Cart: Current cart
3. Controller: Current controller instance
Administrator Context access only below objects:
1. Employee: Current employee
How to access the Context?
The Context access from inside controller or module subclass. You can access to context in front and back controllers this way: $this->context.
or you can used everywhere: Context::getContext().
For example: $language_id = $this->context->language->id;
How is the Context initialized?
The context is initialized with data coming from the database or cookies.
For example: Create the Currency object, the context looks for an id_currency value in the cookie. When it doesn’t find, it will retrieve the default currency id from the database.
How to use the Context?
Whenever you would have used a global statement or accessed the cookie to retrieve a variable, you will probably want to access the Context instead.
Please check below example to replace the code of PrestaShop 1.4 to PrestaShop 1.5.
For example 1:
$id_cart = self::cart->id;
Replaced with..
$id_cart = $this->context->cart->id;
For example 2:
$language = new Language($cookie->id_lang);
$iso_code = $language->iso_code;
Replaced with..
$iso_code = $this->context->language->iso_code;
More examples of Context use

Old Way
New Way
$language = new Language($cookie->id_lang);
new Currency($cookie->id_lang);
new Link();
$smarty->assign(‘cart’, $this->cart);
$this->context->smarty->assign(‘cart’, $this->context->cart);

Using the Context in a PrestaShop 1.4 and 1.5 module
Add below method to the module’s main class:
// Retrocompatibility 1.4/1.5+
private function initContext()
if (class_exists(‘Context’))
$this->context = Context::getContext();
global $smarty, $cookie;
$this->context = new StdClass();
$this->context->smarty = $smarty;
$this->context->cookie = $cookie;
Then, add this line in the module’s constructor method:
// Retrocompatibility
These are two hooks are compatible for both PrestaShop v1.4 and v1.5.
1. productTab
2. productTabContent

Leave A Comment