Slow OpenCart - quick fix

2011-10-06 13:39:00 CET - kafoso

Do you run or maintain an OpenCart webshop with many products and categorires? And the load times have become horribly long? Then you are in luck, because there is a very easy fix to improve load times significantly.

Slow load times can be caused by a large variety of things, e.g. faulty configuration of the server (IIS, Apache, etc.), PHP not being configured properly, ISP promised you a 10/5 Mbit connection when in fact you have 2/1 Mbit, server receives too much traffic, etc.

But if most things on the server responds quickly and OpenCart is still very slow - specifically the Frontend - then the issue can be tied to the OpenCart installation. Furthermore, if you started experiencing the slowdown after adding many categories and products then the cause of the problem probably is, that OpenCart performs over 300 database requests. And this mayhem of requests is performed every time a page starts loading in the Frontend.


Disabling category requests on all OpenCart pages

In the file catalog/controller/common/header.php OpenCart will request all categories on every page, as it is a header file. The data fetched here will be displayed via the file catalog/view/theme/yourtheme/common/header.tpl.

To test whether disabling this will improve your load times, simply out-comment the blocks of code handling categories.

File: catalog/controller/common/header.php
Out-comment the section below and add a null value to data['categories'].

/*
    // Menu
    $this->load->model('catalog/category');
    $this->load->model('catalog/product');

    $this->data['categories'] = array();

    $categories = $this->model_catalog_category->getCategories(0);

    foreach ($categories as $category) {
        if ($category['top']) {
            $children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
                $data = array(
                     'filter_category_id' => $child['category_id'],
                     'filter_sub_category' => true
                );

                $product_total = $this->model_catalog_product->getTotalProducts($data);

                $children_data[] = array(
                    'name' => $child['name'] . ' (' . $product_total . ')',
                    'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                );
            }

            // Level 1
             $this->data['categories'][] = array(
                 'name' => $category['name'],
                 'children' => $children_data,
                 'column' => $category['column'] ? $category['column'] : 1,
                 'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
             );
        }
    }
*/
$this->data['categories'] = null;

 

In catalog/view/theme/yourtheme/common/header.tpl a condition checks if any categories were put into the $categories array, which is why we must set a null value for data['categories']. Otherwise a PHP error will appear where the menu was.

If this improved your load times I suggest making a "landing page" for categories, e.g. using an Information page and the Category module. This will of course mean your lose the fancy Category dropdown menu, but instead people won't tear the hair from their heads because of the load times and leave the shop without buying anything.