Child pages
  • Modules, Override, Web Service
Skip to end of metadata
Go to start of metadata

Modules, Override, Web Service : Tools You Need to Personalize Your Store!

This article was written by Damien Metzger, and was first published on May 4th, 2011, on the PrestaShop blog.

Ever since the creation of PrestaShop, our modules have been the easiest and most powerful way for merchants to customize their stores. Yet PrestaShop v1.4 released two new possibilities for your store and your business to go further, faster, and be more efficient : let us introduce you to how to override the core and use the web service.

Overriding Core

Overwriting allows all developers, whether they are freelancers or work for a web agency, to change the behavior of classes that make up the PrestaShop core—without modifying it! You can thus also extend the PrestaShop code, either to replace it with its own code, optimized to meet the store’s needs, for example, or simply to add instructions to the existing ones.

Now with version 1.4, a developer who wishes to modify a PrestaShop class no longer touches the core files. Instead, they create a file in the folder /override that appears at the root of the software.

Example

To add a message such as “Hello world!” each time you load a product—which is perfectly useless, of course—you just need to place the file Product.php in the folder /override/classes, and in the file you must write:

class Product extends ProductCore
{
  public function __construct($id_product = NULL, $full = false, $id_lang = NULL)
  {
    echo 'Hello World !';
    parent::__construct($id_product, $full, $id_lang);
  }
}

Thus, a technician who works on your store can immediately identify the modifications made. Furthermore—and this is really important!—updating PrestaShop to a future, improved version will be considerably easier. You will just have to test these overridden classes’ compatibility rather than doing a tedious and time-consuming diff with files from the new version.

If you’re not comfortable with the concept of Inheritance (object-oriented programming)—which is necessary for creating a child class—you should take a look at some information on Wikipedia: http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

Web service

Web services are increasingly present in all areas of computing, and are essential to the ongoing evolution of the web, and therefore essential to e-commerce as well.

When applied to PrestaShop, web services enable developers, whether familiar or not to developing with PrestaShop, to remotely retrieve and send information to the store without having access to the software code!

The technology that PrestaShop has chosen for its web service is REST. The principal advantage of this style of architecture is that it’s based on the XML format. The XML files exchanged between the client and the web service are made to be used intuitively, without first having to read several pages of documents specific to the application. A developer familiar with this technology can easily use the service, and a beginner will adapt very quickly.

PrestaShop offers developers a PHP library which allows them to use the web service without needing to make XML files by hand. Various example codes are also offered to better understand the different options.

A step-by-step guide on the web service is available on PrestaShop’s documentation site: http://doc.prestashop.com/

You can find out more about REST on Wikipedia: http://en.wikipedia.org/wiki/Representational_State_Transfer

How to choose?

While these three tools let everyone modify the behavior and add features to a store, they also have three very different applications. Choosing the right tool is therefore not to be taken lightly.

If you want to make specific changes to your store, which will never be used elsewhere, then in most cases choosing to overwrite the core—classes or controllers—will be the best solution. It is indeed the most natural and efficient way to make changes. You will nevertheless need to monitor its operation after an update, especially if corrections were made to the features you have overwritten, or if some of them are outdated.

What if instead of making changes to your one store, you intend to package and distribute your changes? For example, you may wish to place them on the website for PrestaShop Addons downloads. In this case, you need to choose a module. Using a module also helps keep things clean: its hook system lets us immediately and easily see what changes were made to every event started by the store. Someone looking at the code, in order to use or improve it, could immediately identify the module’s scope of action.

Last but not least, the web service is the best choice when the store must interact with external software. This technique not only allows the third party application to communicate with the store without adding code, but it can also protect confidential information by limiting rights.

  • No labels