Page 1 of 1

[Tutorial] Build Opencart Theme

Posted: Fri Jul 22, 2011 5:16 pm
by qahar
Opencart use fallback function, it's mean that when opencart doesn't found certain template in your theme, it will find on default theme folder. So to make a new theme, you doesn't need to copy all file from default theme. But, building a theme not just about make new folder theme and change it color. In this tutorial we will learn basic understanding on how controller and model work, it's related to template modification.

Before we continue, I want to make it clear that theme in this tutorial is refer to the theme inside theme folder (catalog/view/theme/mytheme) and template refer to .tpl file inside the template folder (catalog/view/theme/yourtheme/template).

Step 1. Build "Very" Basic Theme
    1. Create new folder mytheme on catalog/view/theme/, the folder tree will be like this:
      • catalog/view/theme/
        • |-> default
          |-> mytheme
    2. Now go to Admin -> System -> Setting - > Edit Store ->Tab Store -> template -> mytheme.
      Refresh your frontpage. Maybe your site litle bit mess, but your new theme is work!! :)
Step 2. Basic Theme
    1. Make folder and copy some files from default theme, but DO NOT copy all files. Follow this folder tree:
      • catalog/view/theme/
        • |-> default
          |-> mytheme
          • |-> image/*.* - copy all image
            |-> stylesheet/*.* - copy all stylesheet
            |-> template
            • |-> common
              • |-> header.tpl
      1. Note:
      2. We need to copy all the images because it's required by stylesheet.css.
      3. We need to copy IE stylesheet since it's declared on header.tpl (remove the file when you removing the IE style at header.tpl)
      4. We neet to copy slideshow.css and carousel.css since it's needed by opencart module.
      5. Rating star image is hard-coded into Page: category, manufacturer_info, product, review, search, special; Module: bestseller, featured, latest, special. It's up to you whether including those module and page to your theme and used another rating image, or just replacing use default rating star image.
    2. Now open header.tpl with text editor.
    3. Search word default and replace with mytheme
    4. Refresh your frontpage, and everything should be the same as when you used the default theme.
    5. To get different visual like changing color etc, you can modificate mytheme/stylesheet/stylesheet.css
Step 3. Customizing Template (1): Understanding Controller
    1. What template (*.tpl) is need to customize for a "good theme" ? Well, the answer is relative. In step 2 we already and only customizing the header.tpl. The most important rule to remember is never edit default theme template. Copy what you need to your theme folder, see example bellow.
      • catalog/view/theme/
        • |-> default
          |-> mytheme
          • |-> image
            |-> stylesheet
            |-> template
            • |-> common
              • |-> header.tpl
                |-> footer.tpl
              |-> information
              • |-> information.tpl
              |-> product
              • |-> product.tpl
                |-> category.tpl
                |-> manufacturer_list.tpl
    2. To customizing template and work with the controller, you need to understand that opencart used push-based MVC model -CMIIW.
      In quick explanation:
      1. When you accessing route=product/category url, opencart call controller/product/category.php file.
      2. This controller (ex. category.php) will decide which MV-L: Model, View (tpl), language will be load. In category controller (category.php) load:
        1. 3 Model (category, product, image): $this->load->model('...');
        2. 2 View (category.tpl & not_found.tpl): $this->template = '...';
        3. 1 Language: $this->language->load('...')
      3. The controller also decide what data will be pushed into template and how user input will be processed.
        1. $this->$this->data['text_price'] = $this->language->get('text_price'); will produce Price in template: <?php echo $text_price; ?>
        2. When you change the product show (from 15 to 25) at frontpage, controller catch the request with if (isset($this->request->get['limit'])) { ... } then process it $this->data['limits'][] = array(... 'value' => 25, ...);
    3. Remember that there is no fallback function for controller. If you modificate the controller file manually, it will be replaced when you upgrade the opencart. Instead modificate it manually, you can used vQmod to make "virtual modification". We will talk this on step 5.
Step 4. Customizing Template (2): Understanding Model
    1. A Model in MVC in charge to pull and push data to database. Before controller get or post a data through model, the spesific model need to be loaded.
      1. load model: $this->load->model('catalog/product');
      2. get data: $this->model_catalog_product->getTotalProducts()
      3. post data: $this->model_catalog_product->editProduct()
    2. $this->load->model('catalog/product') tell the opencart to load model/catalog/product.php either in admin or catalog controller. getTotalProducts(), editProduct() is a function inside the model/catalog/product.php.
    3. Now open model/catalog/product.php and find public function getProduct.
      1. See list after return array, and you will found all product data.
      2. The question is, if the getProduct() listed all product data, why it doesn't show at category page (frontpage)? This because the category controller decide not to show all data.
      3. Open controller/product/category.php, find $this->data['products'][] = array to see what product data is used by controller.
Step 5. Customizing Template (3): Understanding vQmod
    1. vQmod is a virtual modificate and usually used to make some change to non-fallback file like controller and model without modificating the default file.
    2. You can download latest version and read further explanation about vQmod here.
    3. To install vQmod, copy vqmod folder inside the package to opencart root.
      • yoursite
        • |-> admin
          |-> catalog
          |-> download
          |-> image
          |-> system
          |-> vqmod
    4. Go to your browser and access: http://localhost/yoursite/vqmod/install. You will see success message: vQmod has been installed on your system!
    5. On the vQmod package, you will see folder docs and example to give you a refference how vQmod work. Here I will give you some quick refference:
      1. vQmod File is an .xml file stored at vqmod/xml folder. When executed, the vQmod force opencart to use the modification instead the default file (original file) and produce cache file at vqmod/vqcache.
      2. One vQmod File able to modificate multiple file; within one file, vQmod able to do multiple modificate operation.
      3. Example structure inside a vQmod File:
        • <id>vQmod File ID</id>
          <version>1.0.0</version> --> vQmod File version
          <vqmver>1.0.8</vqmver> --> minimum vQmod version to work
          <author>your name</author>
          <file name="catalog/controller/product/category.php "> --> the file to modify
          • <operation>
            • <search position="replace"><![CDATA[
              search this code and replace it with code bellow
              add this new code to replace code above
            • <search position="after"><![CDATA[
              search this code and add code bellow after it
              add this new code after code searched above
          <file name="...">
          • <operation>
            • <search position="before"><![CDATA[
              search this code and add code bellow before it
              add this new code before code searched above

Since most user unable to reply this post, if you want to suggest think to improve this tutorial you can PM me. No Question Please!
Make new post on Support Forum if you have any question.

Re: [Tutorial] Build Opencart Theme

Posted: Thu Mar 01, 2012 6:09 pm
by qahar
How to use include, require, include_once or require_once that support vQmod?

If you use something like this on your theme:

Code: Select all

include 'bla_bla.tpl';
vQmod will unable to modificate your included file. To make vQmod able modify the file, you should use the full path. Qphoria on this post suggest to use $vqmod->modCheck():


Code: Select all

Instead use:

Code: Select all