Override PrestaShop's jQuery and use Google's CDN

Speed up your PrestaShop store by getting Google to host jQuery for you with this PrestaShop override

Open /override/classes/controller/Controller.php.

If it has never been edited it should look like this:

<?php

abstract class Controller extends ControllerCore
{

}

Between the curly brackets, or after any other overrides you may have add:

/**
 * Use Google's CDN to host jQuery
 * @param string  $version  Version of jQuery to include
 * @param string  $folder   Not used in this override
 * @param boolean $minifier Use minified version?
 */
public function addJquery($version = null, $folder = null, $minifier = true) {

    $this->addJS(Media::getJSPath(Tools::getCurrentUrlProtocolPrefix() . 'ajax.googleapis.com/ajax/libs/jquery/' . ($version ? $version : _PS_JQUERY_VERSION_) . '/jquery'.($minifier ? '.min.js' : '.js')));

}

Your /override/classes/controller/Controller.php file should now look something like this:

<?php

    abstract class Controller extends ControllerCore
    {

        /*** All other previous overrides here ***/

        /**
         * Use Google's CDN to host jQuery
         * @param string  $version  Version of jQuery to include
         * @param string  $folder   Not used in this override
         * @param boolean $minifier Use minified version?
         */
        public function addJquery($version = null, $folder = null, $minifier = true) {

            $this->addJS(Media::getJSPath(Tools::getCurrentUrlProtocolPrefix() . 'ajax.googleapis.com/ajax/libs/jquery/' . ($version ? $version : _PS_JQUERY_VERSION_) . '/jquery'.($minifier ? '.min.js' : '.js')));

        }
    }

Save the file, refresh and view the source of PrestaShop store, you should see the jQuery library included like this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Congratulations, you're now using Google's CDN to host your jQuery library!

You can read about the benefits in our article Using PrestaShop jQuery 1.7 with Google CDN Tutorial.

By

Mark Hesketh

Mark is a freelance Web Designer and Developer from Lancashire, England, and has been building e-commerce stores with PrestaShop since 2008.

Check out his website or follow him on Twitter and Google+.