Override PrestaShop Controllers Tutorial

How to use PrestaShop override controllers and add new features to your store, without modifying the PrestaShop core.

One of the biggest improvements to PrestaShop as a developer was the ability in to override controllers and ** develop new features for your store** without modifying or 'hacking' the core code.

We're going to look at how to use PrestaShop override controllers to customise and change your store beyond what PrestaShop offers 'out-of-the-box'.

This is great because:

  • Updating is no problem. Previously, updating PrestaShop would overwrite any changes you had made to the core. This meant you could lose the custom features you had become reliant upon. Using PrestaShop's controller overrides this is no longer a problem.
  • Maintaining your code changes is much easier as your controller overrides are kept separate from PrestaShop's core code.
  • There are many other benefits we go into detail about in another post that you should read.

Overriding a PrestaShop controller

In this example we're going to override the product controller to add a customer style sheet to our product pages. This could be useful for instance when showing different layouts to different groups of customers, but we're going to keep it simple for now.

Product pages in PrestaShop use the controller found at /controllers/ProductController.php.

Go ahead and create a new file in /overrides/controllers/ProductController.php and add the following:

<?php
   class ProductController extends ProductControllerCore {
      public function setMedia() {
         parent::setMedia();
         Tools::addCSS(_THEME_CSS_DIR_.'newstyles.css');
      }
   }
?>

What's happening here?

Lets run through it line by line:

class ProductController extends ProductControllerCore

'Extending' a class is basic PHP technique, meaning your new class ProductController will inherit the methods and properties of the original ProductControllerCore found in /controllers/ProductController.php.

We call our new class

ProductController

as its good practice to code organised making it easier to support.

public function setMedia()

This is the method that we want to override. PrestaShop uses setMedia() to add JavaScript and CSS files to our pages.

parent::setMedia();

Because setMedia() already exists in /controllers/ProductController.php we need to use PHP's special name 'parent'. This simply means our overrides won't replace the original method, but be in addition to them.

Tools::addCSS(_THEME_CSS_DIR_.'newstyles.css');

This is the function that will add our newstyles.css file to the product page with the rest of default CSS files.

That's it!

With the override in place, we can check the source code and see that PrestaShop has listened to our override controller and added the newstyles.css file to the product page.

<link href="/themes/prestashop/css/global.css" rel="stylesheet" type="text/css" media="all" />
<link href="/themes/prestashop/css/product.css" rel="stylesheet" type="text/css" media="all" />
<link href="/themes/prestashop/css/newstyles.css" rel="stylesheet" type="text/css" media="all" />
<link href="/css/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" media="screen" />

Remember, you can use this technique to also add custom JavaScript files to your pages. You could also add jQuery plugins to your theme with ease, or if you'd prefer Google to host jQuery for you.

Note: By using setMedia() rather than simply placing your CSS or JavaScript files in your theme .tpl files as HTML, they can take advantage of PrestaShop's performance features such as CCC (Combine, Compress, and Cache).

Have you tried using PrestaShop's override system? If so, what great new features have you added to your stores?