Implementing OpenCart Modules

Exclusive offer: get 50% off this eBook here
Getting Started with OpenCart Module Development

Getting Started with OpenCart Module Development — Save 50%

Master your OpenCart modules and code! with this book and ebook

$20.99    $10.50
by Rupak Nepali | October 2013 | e-Commerce Open Source

This article by Rupak Nepali, author of Getting Started with OpenCart Module Development, he describe how to make, clone, or write code for the OpenCart extensions like module extensions, shipping extension, payment extension, and total order extensions. With the increase in popularity of OpenCart framework, OpenCart users are requesting more custom modules to fulfill their requirement. Thus it helps programmer to understand how to build OpenCart modules. This article includes:

  • Learning how to clone and write codes for OpenCart modules
  • Understanding the Global Library methods used in OpenCart
  • Developing and customizing modules, pages, order totals, shipping, and payments extensions in OpenCart

(For more resources related to this topic, see here.)

OpenCart is an e-commerce cart application built with its own in-house framework which uses Model-View-Controller (MVC) language pattern thus each module in OpenCart also follows the MVCL patterns. Controller creates logics and gathers data from Model and pass it to display them in the view OpenCart modules have admin/ and catalog/ folders and files in admin/ folder helps in controlling setting of module and files in catalog/ folder handles the presentation layer (front-end).

Learning how to clone and write codes for Opencart Modules

We assume that you already know PHP and have already installed the OpenCart and familiar with the OpenCart backend and frontend as well as some coding knowledge with PHP.

You are going to create Hello World module which just has one input box at the admin, settings for the module and same content are shown in front end. First step on module creation is using a unique name, so there will not be conflict with other modules. The same unique name is used to create the file name and class name to extend controller and model.

There are generally 6-8 files that need to be created for each module, and they follow a similar structure. If there is interaction with the database tables, we have to create two extra models. The following screenshot shows the hierarchy of files and folder of OpenCart module.

So now you know the basic directory structure of OpenCart module. The file structure is divided into two sections admin and catalog. Admin folders and files deal with the setting of the modules and data handling while the catalog folders and files handles the frontend.

Let's start with an easy way to make the module. You are going to make the duplicate of the default google_talk module of OpenCart and change it to the Hello World module. We are using the Dreamweaver to work with files.

Changes made at the admin folder

  1. Go to admin/controller/module/ and copy google_talk.php and paste in the same folder and rename it to helloworld.php and open it to your favorite text editor, then find the following lines:

    classControllerModuleGoogleTalk extends Controller {

    Change the class name to

    classControllerModuleHelloworld extends Controller {

  2. Now look for google_talk and replace all with helloworld as shown in the following screenshot:

  3. Then, save the file

  4. Go to admin/language/english/module/ and copy google_talk.php and paste in the same folder and rename it to helloworld.php and open it. Then look for the following line of code:

    $_['entry_code'] = 'Google Talk Code:<br /> <span class="help">Goto <a href="http://www.google.com/talk/service/badge/New" target="_blank"> <u>Create a Google Talk chatback badge</u> </a> and copy &amp; paste the generated code into the text box. </span>';

  5. Replace with the following line of code:

    $_['entry_code'] = 'Hello World Content';

  6. Then again find Google Talk and replace all with Hello World, and save the file.

  7. Go to admin/view/template/module/ and copy the google_talk.tpl file and paste in the same folder and rename it to helloworld.tpl and open it and then find google_talk and replace it with helloworld then save it.

Changes made at the catalog folder

  1. Go to catalog/controller/module/ and copy the google_talk.php file and paste in the same folder and rename it to helloworld.php and open it and look for the following line:

    class ControllerModuleGoogleTalk extends Controller {

    change the class name to

    class ControllerModuleHelloworld extends Controller {

  2. Now look for google_talk and replace all with helloworld and save it.

  3. Go to catalog/language/english/module/ and copy the google_talk.php file and paste in the same folder and rename it to helloworld.php and open it and then look for Live Chat and replace it with Hello World then save it.
  4. Go to catalog/view/theme/default/template/module/ and copy the google_talk.tpl file and paste in the same folder and rename it to helloworld.tpl.

With the preceding files and codes change, our Hello World module is ready to be installed. Now log in to the admin section and go to Extensions | Module, look for the Hello World and click on [install] then click on [Edit]. Then type the content that you would like to show at the frontend in Hello World Content field after that click on the Add Module button and provide the setting as per your need and click on Save.

Understanding the global Library methods used in OpenCart

OpenCart has many predefined methods which can be called anywhere like in the controller or in the model and as well as in the view template files as per the need. You can find system level library files at system/library/. We have defined all the library functions so that it is easy for programmers to use it. For example:

$this->customer->login($email, $password, $override = false)

Log a customer in. It checks for the customer username and password if $override is passed false, else only for current logged in status and the e-mail. If it finds the correct entry then the cart entry, wishlist entries are retrieved. As well as customer ID, first name, last name, e-mail, telephone, fax, newsletter subscription status, customer group ID, and address ID are globally accessible for the customer. It also updates the customer IP address from where it logs in.

Developing and customizing modules, pages, order totals, shipping, and payments extensions in OpenCart

We describe the featured module of OpenCart, create feedback module and tips module, and describe and show how codes work and are managed. We helped to learn how to make pages and modules in OpenCart as well as let visualize the uses of database structure; how data are saved as per language, as per store so it helps OpenCart programmers to understand and follow the patterns of Opencart coding style. We describe codes how form works, how to list out the data from database, how edit works in module, and how they are saved. Show them how to code shipping module in OpenCart as well as total order modules and payment modules. We have outlined how templates, models, and controllers work for extensions.

Summary

In this way we have learned how to clone and write codes for OpenCart modules and the changes made at the admin and catalog folder. We also learned the global library methods used in OpenCart. Also, covered all ways to code the OpenCart extensions.

Resources for Article:


Further resources on this subject:


Getting Started with OpenCart Module Development Master your OpenCart modules and code! with this book and ebook
Published: October 2013
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Rupak Nepali

Rupak Nepali, a PHP programmer from Nepal, has been working on OpenCart since 2010 and has completed many projects and made many modules on OpenCart to meet client requirements. He handles http://opencartnepal.com personally as well as updates his personal site http://rupaknepali.com.np with his works Mr. Nepali currently works as a full-time freelancer on oDesk as well as on various freelancer sites. He holds a Bachelor's degree in Computer Information Systems.

Books From Packt


 OpenCart 1.4 Template Design Cookbook
OpenCart 1.4 Template Design Cookbook

OpenCart 1.4: Beginner's Guide
OpenCart 1.4: Beginner's Guide

 Instant E-commerce with OpenCart: Build a Shop [Instant]
Instant E-commerce with OpenCart: Build a Shop [Instant]

Selling Online with Drupal e-Commerce
Selling Online with Drupal e-Commerce

 Building E-Commerce Solutions with WooCommerce
Building E-Commerce Solutions with WooCommerce

Zen Cart: E-commerce Application Development
Zen Cart: E-commerce Application Development

Joomla! E-Commerce with VirtueMart
Joomla! E-Commerce with VirtueMart

 Django 1.2 E-commerce
Django 1.2 E-commerce


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
V
d
q
5
Y
i
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software