Things you should know before starting Prestashop development

Search for a command to run...

This blog post is a great resource for beginners interested in learning how to develop with PrestaShop. Overall, I think this is a great blog post that provides a lot of valuable information for beginners who want to develop PrestaShop with its modules. PrestaShop Modules are a great way to extend the functionality of the store and make it more competitive. With more than 4,000 modules available, you're sure to find one that meets your needs. I would recommend it to those who are thinking of starting a project with PrestaShop.
Check out more details at: https://store.webkul.com/PrestaShop-Extensions.html/
I'm Marwane, also known by moghwan with a lowercase m, a nickname inspired from a french coworker mistakenly called me by Morwane, tweaked it a bit and voilà! The reason I’m writing this post is self explanatory even if it’s not (technically speaking...

Originally published at dev.to Hello dear artisans! Todays post I'll try to share my laravel/laradock workflow while using Nginx, and exploring/setting-up two database engines: postgres/mysql, alongside pgadmin, phpmyadmin or adminer, depending your...

Originally published at dev.to Disclaimer: I’m writing about my experience with major OS (Windows 10, macOs High/Sierra, Ubuntu/Manjaro) using a Solid State Drive. It has a huge impact in term of speed and it could be different from your own expe...

Originally published at dev.to So basically I want to dockerize all my php based projects (Wordpress, Laravel, Symfony...) after switching from OS to another, and Prestashop was among them. So I'm writing this (my first) post so it will be a kind of...

Originally published at dev.to
Note: I'm only talking about PS1.7 version, not 1.6 or ealier.
So basically i'm writing this post for prestashop beginners developers to explain some ways of doing things' basics so they would't be in the 'Aha!' situations multiple times, like I did.
Developping in prestashop is just like programmig in any php framework, it relies heavely on the OOP programming, and MVC architecture with their their folders classes, controllers and themes/<your-theme-name>. you can learn more about folders architecture from here the documentation is very clear once you understand Hooks (because they are the most confusing, and, easy part).
These are the main lines you should know about:
Why overrides? why not changing directly in classes? simply because prestashop/modules updates will completely overwrite your changes.
The classes that you are able to override are all classes insides classes, constrollers and modules folders, and place them under overrides while respecting their original arborescence.
To take effect of your changes you should either clear your cache folder, or just deleting the var/cache/<dev || prod>/class_index.php file which is a simple mapping class of the different classes with their overrides.
For example:
// code/ProductController.php
<?php
# controllers/front/ProductController.php
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
...
class ProductControllerCore extends ProductPresentingFrontControllerCore
{
public function anyFunction()
{
// function shouldn't be private or it won't work
return 'parent value';
}
}
Let's override the class and redifine any public/protected method:
// code/ProductController-override.php
<?php
# overrides/controllers/front/ProductController.php
class ProductController extends ProductControllerCore
{
public function anyFunction()
{
// use parent::anyFunction() to get parent function value
return 'new value';
}
}
Tools classThis class is as, its name saying, a class of tools and utilities functions. you'll find a set of useful static methods you'll use often. Before creating any utility function in your module make sure it's not present in the Tools class.
Hooks, to be simple, help executing some code/change a template, on a specific event/position.
There a two main types of hooks: display & action.
When using a hook in your module just do a quick check if it's compatible with all 1.7.x.x versions or it's a new one, just in case. Or if a hook is renamed you must register the hook for that specific version so you won't tell yourself later why it's not used.
The complete list of hooks is available here
You'll notice a lot of them ducummented with @deprecated tag, some are even from prestashop 1.5 but technically they call the new defined ones.
Why? Backward compatibility. The other reason is, that people are always late to or never update their online store fearing to break it because, if you're non technical owner ad something goes wrong during an upgrade (or even and update sometimes) it'll cost some - if not only your time - money to fix it.
To quickly download a specific PS version directly head to releases list, no need to go to their site or google the desired version.
Faster? head to https://github.com/PrestaShop/PrestaShop/releases/tag/1.7.x.x and replace the x with your version.
Even faster with the direct link: https://github.com/PrestaShop/PrestaShop/releases/download/1.7.x.x/prestashop_1.7.x.x.zip
At first it seemed very complicated to me but in fact, it's simply tricky.
Besides replacing core folder such as classes, controller, src and the classic theme, updating the database is our main focus. Take a look at the folders php and sql inside the folder [/install-dev/upgrade/](https://github.com/PrestaShop/PrestaShop/tree/develop/install-dev/upgrade), notice something? let me explain:
Configuration::updateValue('YOUR_UNIQUE_PARAM_NAME', 'new value');
Configuration::get('YOUR_UNIQUE_PARAM_NAME');
Configuration::deleteByName('YOUR_UNIQUE_PARAM_NAME');
- Get any input value by name, sent in GET, POST or Ajax:
Tools::getValue('input_name')
Context has many useful properties filled with data depending on, the current state, the context! here's some examples:
```php
// code/context.php
// returns the id of the current used language.
$this->context->language->id;
// assign variables from current controller to the view
$this->context->smarty->assign([
'categories' => $categories,
]);
// the view
return $this->display(__FILE__, 'views/templates/admin/someadminhook.tpl');
// fetching the rendered content of a view,
$this->context->smarty->fetch('module:your_module_dir_name/views/templates/front/display.tpl')
// get current cart
$this->context->cart;
Global variable are prefixed with _PS_ and defined in config/defines.inc.php
making a simple ajax call:
// code/ajax/display.php
// returns the id of the current used language.
$this->context->language->id;
// assign variables from current controller to the view
$this->context->smarty->assign([
'categories' => $categories,
]);
// the view
return $this->display(__FILE__, 'views/templates/admin/someadminhook.tpl');
// fetching the rendered content of a view,
$this->context->smarty->fetch('module:your_module_dir_name/views/templates/front/display.tpl')
// get current cart
$this->context->cart;
// code/ajax/front.js
// preparing the ajax call inside a function
function getProductsByCategoryJs(categoryId) {
$.ajax({
url : AJAX_URL,
type : 'POST',
async: true,
dataType : "json",
data: {
action: 'getProductsByCategoryPhp',
categoryId: categoryId,
ajax: 1
},
success : function (response) {
console.log(response);
$('#your-element').html(response.ajaxTpl);
},
error : function (error) {
console.log(error);
},
});
}
// code/ajax/ajax.php
# my_module/controllers/front/display.php
// making the request url for our contoller in initContent() function
$url = Context::getContext()->link->getModuleLink(
'my_module', // module name
'ajax', // contoller name, could be 'display'
array( // we can define more parameters here
// 'action' => 'DoSomeAction',
// 'ajax' => 1,
)
);
// making variable accessible in javascript
Media::addJsDef([
'AJAX_URL' => $url,
]);
There are lots of things to talk about but I tried to put content from here and there for a better view for prestashop beginners (including me) and not getting confused because, it is, kinda.
If you have any recommendation, improvement, found a typo just let me know the comments or fork this project and make your edits, any contribution is welcome.