You are on page 1of 2

How to create extension in openCart 3.

- Go throw the big difference to 2.X

1/ Diferences with 2.X


- CANNOT use install.php
- Directories are allowed for uploading new files:
-admin/controller/extension/
-admin/language/
-admin/model/extension/
-admin/view/image
-admin/view/javascript/
-admin/view/stylesheet/
-admin/view/template/extension/
-catalog/controller/extension/
-catalog/language/
-catalog/model/extension/
-catalog/view/javascript/
-catalog/view/theme/
-system/config/
-system/library/
-image/catalog
- No need to setup the FTP and installer can ve work on localost and hosting
server
- Language text is imported automatically to controller
<?php
$this->load->language('extension/{Directory}/{File}');
?>
- TWIG, this is a big difference between 2.X and 3.0
- Coding to present the data in controller to view file:
2.X (TPL) <?php echo $text; ?>
3.0 (TWIG) {{ text }}
- Not only the text, for loop, ef else methos are also different. official
TWIG site for details
- Admin login 2.X is using "token" and 3.0 "user_token"
- When you create the XML and controller file for any linking this word CANT
be wrong

2/ Tables
- Blog (id, status, sort_order)

3/ Prepare items in root level


- /upload/
- install.xml (if needed)

4/ Create the Admin -> Controller file


- We need to create a new file in /extension/module/ and name it blog.php
- The most important thing we need to create 2 functions in this file are:
- install() & uninstall()
Why? Because openCart 3.0 is not support install.php already. So, if
developer needs to create the database table in exist 3.0 system, it is NEEDED-
<?php
class ControllerExtension{Directory}{File} extends Controller{
public function install(){
// Load the model file with path
$this->load->model('extension/{directory}/{file}');
// Call the function install() in model
$this->model_extension_directory_file->install();
}
public function uninstall(){
// Load the model file with path
$this->load->model('extension/{directory}/{file}');
// Call the function uninstall() in model
%this->model_extension_directory_file->uninstall();
}
}
?>

5/ Create the Admin -> Model file


- You find that we need a model file to support the controller action, so we need
a model file in /model/extension/
- What we need?
<?php
class ModelExtension{Directory}{File} extends Model{
public function install(){
$create_table_sql = array(
"CREATE TABLE `" . DB_PREFIX .
"extension` (`id` int(11) NOT NULL AUTO_INCREMENT, `sort_order`
int(3) DEFAULT 0, `status` tinyint(1) NOT NULL, `image` varchar(255) DEFAULT NULL,
PRIMARY KEY(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
);

foreach ($create_table_sql as $query){


$this->db->query($query);
}
}

public function uninstall(){


$delete_table_sql = array(
"DROP TABLE IF EXISTS `" . DB_PREFIX . "extension`",
);

foreach($delete_table_sql as $query){
$this->db->query($query);
}
}
}
?>

6/ Create the Admin -> View file


- Remember we need to create the files in TWIG format:
- template/extension/{Directory}/{File}_list.twig
- template/extension/{Directory}/{File}_form.twig

7/ When Admin side is done, we can create the files in catalog


- The steps are the same as 2.X ( ._.) except the view, we neet to create TWIG
FILE

8/ Zip into extension.ocmod.zip with upload and install.xml on root

You might also like