





















































In this article by Rupak Nepali, author of the book OpenCart Theme and Module Development, we learn about the basic features of OpenCart and its functionalities. Similarly, you will learn about global methods used in OpenCart.
(For more resources related to this topic, see here.)
The latest version of OpenCart at the time of writing this article is 2.0.1.1, which boasts of a multitude of great features:
Its pre-existing features include the following:
It also supports:
It is search engine optimized and has backup and restoration tools. It provides features such as printable invoices, sales reports, error logging, multistore features, multiple marketplace integration tools such as OpenBay Pro, and many more.
Now, let's start with some basic general setting that will be helpful in creating our theme and module.
The following can be the advantages of using Bootstrap in an OpenCart 2 theme:
Here are a few things to take care of before stepping into HTML and CSS to create an OpenCart theme:
The following steps help create a new theme based on the default theme:
Now we can make changes to the CSS or the template files, and see the changes. Sometimes, we need theme-specific JavaScript. In such cases, we can create a javascript folder, or another similar folder as per the requirement.
OpenCart has many predefined methods that can be called anywhere, for example, in controller, model, as well as view template files. You can find system-level library files at system/library/. We will show you how methods can be written and what their functions are.
You can find most of the affiliate code written in the affiliate section. You can check out the files at catalog/controller/affiliate/ and catalog/model/affiliate/.
Here is a list of methods we can use for the affiliate library:
$this->affiliate->login($email, $password);
$this->affiliate->logout();
$this->affiliate->isLogged();
if ($this->affiliate->isLogged()){
echo "Welcome to the Affiliate Section";
} else {
echo "You are not at Affiliate Section";
}
$this->affiliate->getId();
The following changes need to be made in the catalogfolder:
classModelShippingWeight extends Model {
Change the class name to this:
classModelShippingTotalcost extends Model {
Now, find weightand replace all its occurrences with totalcost.
After performing the replacement, find the following lines of code:
$totalcost = $this->cart->gettotalcost();
Make the changes as shown here:
$totalcost = $this->cart->getSubTotal();
Our requirement is to show the shipping cost as per the total cost purchased, so we have made the change you just saw.
if ((string)$cost != '') { $quote_data['totalcost_' . $result['geo_zone_id']] = array( 'code'=>'totalcost.totalcost_'.$result['geo_zone_id'], 'title'=>$result['name'].'('.$this->language->get('text_ totalcost') . ' ' . $this->totalcost->format($totalcost, $this- >config->get('config_totalcost_class_id')) . ')', 'cost' => $cost, 'tax_class_id' => $this->config->get('totalcost_tax_class_id'), 'text'=> $this->currency->format($this->tax->calculate($cost, $this->config->get('totalcost_tax_class_id'), $this->config- >get('config_tax'))) ); }
In them, consider the following lines:
'title'=>$result['name'].'('.$this->language->get('text_
totalcost') . ' ' . $this->totalcost->format($totalcost, $this-
>config->get('config_totalcost_class_id')) . ')',
Make this change, as we only need the name:
'title' => $result['name'],
Weight has different classes, such as kilogram, gram, pound, and so on, but in our total cost purchased, we did not have any class specified so we removed it.
With these changes, the module is ready to install. Go to Admin | Extensions | Shipping, and then find Total Cost Based Shipping. Click on Install and grant permission to modify and access to the user. Then, edit to con figure it. In the General tab, change the status to Enabled.
Other tabs are loaded as per the geo zones setting.
The default geo zones for OpenCart are set as UK Shipping and UK VAT.
Now, insert the value for Total Cost versus Rates. If the subtotal reaches 25, then the shipping cost is 10; if it reaches 50, then the shipping cost is 12; and if it reaches 100, then the shipping cost is 15. So, we have inserted 25:10, 50:12, 100:15. If the customer tries to order more than the inserted total cost, then no shipping is activated.
In this way, you can now clone the shipping modules and make changes to the logic as per your requirement.
Let's begin by creating tables in the database. We all know that OpenCart has multistore abilities, supports multiple languages, and can be displayed in multiple layouts, so when creating a database we should take this into consideration. Four tables are created: feedback, feedback_description, feedback_to_layout, and feedback_to_store. A feedback table is created for saving feedback-related data, and feedback_description is created for storing multiple-language data for the feedback. A feedback_to_layout table is created for saving the association of layout to feedback, and a feedback_to_store table is created for saving the association of store to feedback.
When we install OpenCart, we use oc_ as a database prefix as shown in the following image and query. A database prefix is defined in config.php where mostly you find something such as define('DB_PREFIX', 'oc_'); , or what you entered when installing OpenCart. We create the oc_feedback table. It saves the status, sort order, date added, and feedback ID. Then we create the oc_feedback_description table, where we will save the feedback writer's name, feedback given, and language ID, for multiple languages. Then we create the oc_feedback_to_store table to save the store ID and feedback ID and keep the relationship between feedback and whichever store's feedback is to be shown. Finally, we create the oc_feedback_to_layout table to save the feedback_id and layout_id to show the feedback for the layout you want. This diagram shows the database schema:
Go to catalog/view/theme/default/template and create a feedback folder. Then create a feedback.tpl file and insert this code:
<?php echo $header; ?>
<div class="container">
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo
$breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
The preceding code shows the breadcrumbs array. The following code shows the list of feedback:
<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1><?php echo $heading_title; ?></h1>
<?php foreach ($feedbacks as $feedback) { ?>
<div class="col-xs-12">
<div class="row">
<h4><?php echo $feedback['author']; ?></h4>
<p><?php echo $feedback['description']; ?></p>
<hr />
</div>
</div>
<?php } ?>
This shows the list of feedback authors and descriptions, as shown in the following screenshot:
To show the pagination in the template file, we have to insert the following lines of code into whichever part we want to show the pagination in:
<div class="row">
<div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
<div class="col-sm-6 text-right"><?php echo $results; ?></div>
</div>
It shows the pagination in the template file, and we mostly show the pagination at the bottom, so paste it at the end of the feedback.tpl file:
<?php if (!$feedbacks) { ?>
<p><?php echo $text_empty; ?></p>
<div class="buttons">
<div class="pull-right"><a href="<?php echo $continue; ?>"
class="btn btn-primary"><?php echo $button_continue; ?></a></div>
</div>
<?php } ?>
If there are no feedbacks, then a message similar to There are no feedbacks to showis shown, as per the language file:
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
<?php echo $footer; ?>
In this way, the template file is completed and so is our feedback management. You can create a module and show it as a module as well. To view the list of feedback at the frontend, we have to use a link similar to http://www.example.com/index.php?route=feedback/feedback, and insert the link somewhere in the templates so that visitors will be able to see the feedback list.
Like this, you can extend to show the feedback as a module, and create a form at the frontend from which visitors can submit feedback. You can find these at demo code. Try this first and check out the code if you need any help. We have made the code files as descriptive as possible.
Using OpenCart themes, you can customize the presentation layer of OpenCart. Likewise, if you can code OpenCart's extensions or modules, then you can also customize the functionality of the OpenCart e-commerce framework and make an e-commerce site easier to administer and look better
Further resources on this subject: