Ok, extensions have a few extra steps.
First are you doing this in 1.0 or 0.7.x? 1.0 really isn't ready for development and it doesn't actually support modules since daniel hasn't added that back in. So just give me the OK and I will move this to the 0.x forum.
Back to the point, and assuming 0.7.x...
Step 1. To create a new module, you will need to create the follow file structures.
Front-end files:
- catalog/extension/module/xxx.php
- catalog/language/english/extension/module/xxx.php
- catalog/template/default/module/xxx.tpl
Back-end files:
- admin/controller/module_catalog_xxx.php
- admin/extension/module/xxx.php
- admin/language/english/controller/module_catalog_xxx.php
- admin/template/default/content/module_catalog_xxx.tpl
I would clone one of the simpler existing modules like "footer", and just modify the files for the new module.
Step 2. We will start with the front-end files first.
Edit catalog/extension/module/
xxx.
php. This is where the main controlling code goes. all the calculations, or data is handled here.
Using footer.php file as our example, you can see the only real code that is being done here is to pass a variable from the controller to the view called:
text_powered_by. This variable is set with the content that is pulled from the language file referred to by a variable there with the same name. Change this to be code for what you want to do.
locator->get('config');
$language =& $this->locator->get('language');
if ($config->get('footer_status')) {
$language->load('extension/module/footer.php');
$view = $this->locator->create('template');
$view->set('text_powered_by', $language->get('text_powered_by'));
return $view->fetch('module/footer.tpl');
}
}
}
?>
Step 3. Now that you have your code, you need to add the supporting context to the language file.
Edit catalog/language/english/extension/module/
xxx.
php. Everywhere you call a reference to the language file, for example: $view->set('text_powered_by',
$language->get('text_powered_by'));, you need to create a matching name in the language file with the message you want:
(from: catalog/language/english/extension/module/footer.php):
$_['text_powered_by'] = 'Powered by OpenCartCopyright © '.date('Y');
Notice the language file can support full html, a great place to handle the links and style.
Save the file and close it.
Step 4. In the main extension, you needed to create a $view->set for every variable you wanted the template to be able to display. Now you need to match those corresponding variable names in the template.
Edit catalog/template/default/module/
xxx.
tpl.
Examining the first part of our example:
$view->set('text_powered_by', $language->get('text_powered_by'));
Now the view can use the variable
$text_powered_by.
(from: catalog/template/default/module/footer.tpl):
This is where the majority of your html will go, using php tags to reference the preset view variables
Make the needed changes.
Save the file and close.
Step 5. Edit catalog/template/default/layout.tpl
Follow the existing structure for the other modules.
Add the following block where you want it:
Now we have our front-end files done. But we need to add it to the admin area for enable/disable control and add it to the database.
Step 6. First you will need to add it to the database via the admin panel. This is somewhat of a mundane process, as it would be best if the code could just read the directory and find the new files (planned for a future release). Take a look at the existing footer module in the admin area under "Extensions->Module". Click the "update" button (icon looks like a notepad) and click the 'data' tab. You should see the configuration settings that look something like this:
Code: Select all
Code: footer
Directory: module
Filename: footer.php
Controller: module_catalog_footer
Using that example. Go back to the main "Extensions->Modules" menu and click the "Insert" button at the top.
- Enter the name of the module
- enter the data information using the example from above.
- Save it.
Step 7. Edit the new admin/extension/module/
xxx.
php file and change all places you see "footer" with the new name. Unless you have some additional settings (advanced configurations only), this file is basically generic template.
Save & close.
Step 8. Edit admin/language/english/controller/
module_catalog_xxx.
php
Once again, change all places that say "footer" to the new name
Save & close.
Step 9. Edit admin/template/default/content/
module_catalog_xxx.
tpl
And again, change all places from "footer" to the new name.
Save & close.
Step 10. Finally, go back to the "Extensions->Module" page you should be back at the main modules screen again. Click the "wrench" or "spanner" icon, and enable the module.
Now you should have a working module!