Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Core concepts

The same words come up all the time in PrestaShop documentation and code: front office, back office, hook, context. This lesson is a vocabulary. Each concept is covered in detail later; here it matters to see what is what.

Front office and back office

PartWho uses itShort name
Front office (the storefront)The customer: catalogue, cart, orderFO
Back office (the admin panel)The shop owner: products, orders, settingsBO

These are two separate parts with different addresses, templates and controllers. A module usually works in both: it prints a block in the front office and offers a settings page in the back office.

Module, hook, theme

ConceptWhat it is
ModuleA folder in modules with a main class. It can be installed, disabled and removed from the back office
HookA named point in code or in a template where the engine asks modules: "does anyone want to add something?"
ThemeThe templates, styles and scripts of the front office

They are connected like this: the theme calls a hook, for example displayHome, at the right place of a page, and a module subscribed to that hook returns HTML. The theme does not know which modules are installed, and the module does not know how the theme is built.

Two cores

PrestaShop is gradually moving to the Symfony framework, so two worlds live in the code.

Legacy coreNew core
Foldersclasses, controllerssrc
TemplatesSmarty, .tpl filesTwig, .html.twig files
Where it worksThe whole front office and part of the back officeMost back office pages
What the code looks likeClasses without namespaces: Product, DbSymfony services, routes, controllers

A module has to deal with both: a front office block is written the legacy way, a back office page the new way. The course always says which world an example belongs to.

Context

The context is an object of the Context class that holds everything about the current request: which shop, language and currency, who the customer is and what cart they have.

$context = Context::getContext();
 
$languageId = (int) $context->language->id;
$currencyCode = $context->currency->iso_code;
$customerName = $context->customer->firstname;

The first line gets the context, the others read the language id, the currency code and the customer's name from it. Inside a module and a controller there is no need to call getContext(): the context is already in $this->context.

What the context holds

PropertyWhat is in it
languageThe current language
currencyThe current currency
shopThe current shop
customerThe customer in the front office
cartTheir cart
employeeThe employee in the back office
linkThe builder of page addresses
smartyThe template engine
controllerThe controller of the current page

Several shops and languages

One installation can serve several shops and languages. That is why a product has no single name: it has one per language. That is also why many core methods take a language id and a shop id, which are usually taken from the context.

Common mistake

Code in the back office reads $this->context->customer or $this->context->cart and gets an empty object: there is no customer or cart there, there is an employee in $this->context->employee. In the front office it is the other way round. Before reading a context property, think about which side the code runs on.

Thanks for reading!

Donate to the site — we work hard for you.