Integration Opencart  website with CDN

Integration with our CDN service in Opencart is not done using modules or plugins, but by changing three files via FTP/SSH.

7 days free

Connecting your site to CDN allows you:

  • speed up your website's loading time in different regions;
  • optimize images on your website on the fly;
  • significantly reduce outgoing traffic from your server;
  • reduce the number of requests to your server;
  • reduce the cost of AntiDDoS for your server.

All this is achieved by automatically transferring cache copies of images, styles, and scripts to numerous cdnnow! CDN servers.

Important! Integration with our CDN service in Opencart is not done using modules or plugins, but by changing three files via FTP/SSH. First, make sure you can connect to your website via FTP or SSH (where the website files are located).

Creating a CDN Account

If you don't yet have a cdnnow! account, submit a CDN activation request, by providing your email, website, and CMS.

You will receive an email with access to your Control Panel and a default CDN address for your project in the format userXXXXX.nowcdn.co or userXXXXX.clients-cdnnow.ru, where userXXXXX — is your unique identifier.

Connecting CDN to Opencart version 2.x

  1. Make sure you made a backup of these files on your site:
    /config.php
    /catalog/model/tool/image.php
    /catalog/view/theme/default/template/common/header.tpl
  2. Edit the /config.php file, located in the root directory of your website. Here we will specify the CDN address.

    Add the following line anywhere in the file:

    define('CDN_HTTPS_SERVER', 'https://userXXXXX.nowcdn.co/');

    Replace userXXXX with your project address. The slash at the end is required. The https protocol can be replaced with http in the URL if your site only works with http.

  3. Edit the file /catalog/model/tool/image.php (here we will specify the path to the images on the site).

    Find the following fragment in the file code:

    if ($this->request->server['HTTPS']) {
        return $this->config->get('config_ssl') . 'image/' . $new_image;
    } else {
        return $this->config->get('config_url') . 'image/' . $new_image;
    } 

    Replace it with this code fragment:

    if (defined('CDN_HTTPS_SERVER') and ! empty(CDN_HTTPS_SERVER)) {
        return CDN_HTTPS_SERVER . 'image/' . $new_image;
    } else {
        if ($this->request->server['HTTPS']) {
            return $this->config->get('config_ssl') . 'image/' . $new_image;
        } else {
            return $this->config->get('config_url') . 'image/' . $new_image;
        }
    } 
  4. Edit the file /catalog/view/theme/default/template/common/header.tpl. This file defines the paths to styles and scripts. By default, this file contains relative paths to these files, starting from the catalog directory. Example of website code before connecting to CDN::
    File URLs in Opencart without CDN

    Insert the following code before the word catalog:

    <?php if (defined('CDN_HTTPS_SERVER') and ! empty(CDN_HTTPS_SERVER)) { echo CDN_HTTPS_SERVER; } ?>

    The result will be a string like this (the script is connected via CDN):

    <script src="<?php if (defined('CDN_HTTPS_SERVER') and ! empty(CDN_HTTPS_SERVER)) { echo CDN_HTTPS_SERVER; } ?>catalog/view/javascript/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>

    Do the same with the other js scripts and css styles in this file.

  5. Upload the modified files via FTP and refresh your website page. Check that the website is displayed correctly.
  6. Make sure that the URLs to scripts, styles, and images in the website's HTML code contain the CDN address.
    CDN file URLs

This completes the Opencart 2.x setup and the CDN is ready to use.

Connecting CDN to Opencart version 3.x

  1. Make sure to make a backup of the following files on your site:
    /config.php
    /catalog/model/tool/image.php
    /catalog/view/theme/default/template/common/header.twig
  2. Edit the file /config.php, located in the root directory of your website. Here we will specify the CDN address.

    Add the following line anywhere in the file:

    define('CDN_HTTPS_SERVER', 'https://userXXXXX.nowcdn.co/');

    Replace userXXXX with your project address. The slash at the end is required. The https protocol can be replaced with http if your site only works with http.

  3. Edit the file /catalog/model/tool/image.php (here we will specify the path to the images on the site)

    Find the following fragment in the file code:

    if ($this->request->server['HTTPS']) {
        return $this->config->get('config_ssl') . 'image/' . $image_new;
    } else {
        return $this->config->get('config_url') . 'image/' . $image_new;
    } 

    Replace it with this code fragment:

    if (defined('CDN_HTTPS_SERVER') and ! empty(CDN_HTTPS_SERVER)) {
        return CDN_HTTPS_SERVER . 'image/' . $image_new;
    } else {
        if ($this->request->server['HTTPS']) {
            return $this->config->get('config_ssl') . 'image/' . $image_new;
        } else {
            return $this->config->get('config_url') . 'image/' . $image_new;
        }
    } 

    Please note that if you have upgraded from Opencart 2.x to 3.x, the spelling of the $image_new variable has changed in this file (in version 2.x it was called $new_image).

  4. Edit the file /catalog/view/theme/default/template/common/header.twig (this is where the paths to styles and scripts are defined).

    Please note if you have upgraded from Opencart 2.x to 3.x: in version 3.x, Opencart switched from tpl to twig templates, so the syntax will change relative to the second version of the CMS.

    By default, this file contains relative paths to js and css, starting from the catalog directory.

    Example of website code before connecting CDN:

    File URLs in Opencart without CDN

    Insert the code before the word catalog:

    {{ constant('CDN_HTTPS_SERVER') }}

    You will get a string like this (script connected via CDN):

    <script src="{{ constant('CDN_HTTPS_SERVER') }}catalog/view/javascript/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>

    Do the same with the other js scripts and css styles in this file.

  5. Upload the modified files via FTP and refresh your website page. Check that the website is displayed correctly.
  6. Make sure that the URLs to scripts, styles, and images in the site's HTML code contain the CDN address.
    CDN file URLs

This completes the Opencart 3.x setup and the CDN is ready to use.

Your domain for CDN

If you want to use your domain to connect to CDN instead of userXXXXX.nowcdn.co, follow the instructions for connecting your domain. In this case, in the /config.php file instead of the line:

define('CDN_HTTPS_SERVER', 'https://userXXXXX.nowcdn.co/');

insert the line:

define('CDN_HTTPS_SERVER', 'https://cdn.example-domain.com/');

where cdn.example-domain.com — is your domain for CDN.

See also:

Cost of CDN services

We provide a free 7 days trial for all our new clients. When you get access to your personal account you also get unlimited access to CDN and image optimization features. During the trial period, you can thoroughly test the download speed; we do not limit the functionality for trial accounts.

If you like our service and are ready for long-term cooperation, we will convert your account from trial to normal.

The cost of placing data in CDN depends on the amount of traffic. You can check your traffic usage during the trial period in your personal account in the “Statistics” section and calculate how much traffic is expected per month. In this section, you can see the volume of requests for image optimization, if you have enabled this feature.

Тарифы

Traffic-Based CDN

Starting at $8/month — includes
1000 GB. Unlimited projects

Monthly Traffic Cost per 1 GB
First TB $0.008
Next 10 TB $0.020
Over 10 TB on request
from $8 / month
Cost Calculator

Minimum monthly prepayment of $8. Includes 1000 GB of traffic. Excess usage is billed postpaid on a tiered pricing model. Enter your monthly traffic volume in gigabytes. 1 TB = 1000 GB.

GB
1000 10 000
$8 / month
Image Optimization

Each plan includes up to 100 000 optimization requests per month

free 100 000 requests

then $1.12 / 10 000 requests

If you have any questions about setup, maintenance, or pricing, you can always contact our support team:

We use cookies to improve your experience on the website.