Post by Qphoria » Tue Dec 23, 2008 11:20 pm

How is OpenCart structured? / How do I add/change content on my site?

OpenCart uses a pseudo MVC architecture. You can learn more about MVC from google, but for OpenCart, the breakdown is like this:

There are 2 halves to OpenCart. "admin" & "catalog". It is pretty clear what each half does. But the best part about it, is that they both have the same functionality and structure. There is also a common file set between them called the "library". Files inside library are used to create the structures and globals needed for the rest of the cart, as well as handle any core functions.

To keep it simple for this example, i will be working with the catalog side.

There are 3 main parts to each page that you see

1. Controller
This is the core file where all the calculations, dbqueries, and magic take place. This is also where the variables for values and language are set and passed to the view variables for display. Controller files are located in the "controller" directory under catalog & admin depending on which you are looking to modify

2. Language
This is where languages are set up. Language files use a 'constant=value' configuration. The constant name is used in the code, and never changes. Only the value for that language changes. For example:
ENGLISH: $_['text_review'] = 'Product Review';
SPANISH: $_['text_review'] = 'De Revisión de Producto';
GERMAN:  $_['text_review'] = 'Produkt Bewertung';

Notice that the left side (constant) doesn't change. Only the value does for the particular language. Language files are located under the language directory.

3. View

This refers to the template or tpl files. All variables that are passed from the controller to the view can be used for displaying the output of calculations or functionality. For example, if you want to display the current cart total, you would first need to define it to the view from the controller. Some examples:

$view = $this->locator->create('template'); set('entry_page', $language->get('entry_page')); set('text_review_by', $language->get('text_review_by')); <-- Sets a variable called "text_review_by" to the value pulled from the language file for the constant matching that variable name

View files are located under the template directory.  Simple huh?


Putting it all together
Most controller files have a fairly intuitive name to explain what they represent:
product.php = the product page
cart.php = the cart page
home.php = the home page
They also have similarly named files for their corresponding language and view files


Lets say we want to add a new message to the home page that says "Happy Holidays"

First we will edit our Controller file:
EDIT: catalog/controller/home.php

Then we will find an existing variable to use as a guide. There are usually already some variables being passed to the view so no need to recreate the wheel
FIND:

Code: Select all

$view->set('text_latest', $language->get('text_latest'));
AFTER, ADD:

Code: Select all

$view->set('text_[color=red]holiday[/color]', $language->get('text_[color=red]holiday[/color]'));
DONE!

Now we will add that new value to our Language file:
EDIT: catalog/language/english/controller/home.php

Then we will create the new constant and a value for it
FIND:

Code: Select all

$_['text_latest']   = 'Latest Products';
AFTER, ADD:

Code: Select all

$_['text_[color=red]holiday[/color]']   = 'Happy Holidays to your family from www.MySite.com!';
DONE!

Finally, we will add it to the view/template file:
EDIT: catalog/template/default/content/home.tpl

Then add the new variable in the view where we want it displayed. We probably want to list it above where the products start, but below the greeting message.
FIND:

Code: Select all

<?php echo $text_greeting; ?>
AFTER, ADD:

Code: Select all

<?php echo $text_[color=red]holiday[/color]; ?>
DONE!

Now load your home page and you should see your new message displayed.

That's it!
Last edited by Qphoria on Tue Dec 23, 2008 11:37 pm, edited 1 time in total.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: No registered users and 9 guests