How to install the osCommerce Hook system
multimixer | learn | Friday October 30 2015This is a quick explanation about how to install the osCommerce “hook system”. It will be standard in the next osCommerce versions, but you can start using it on your existing store of series 2.3.xx, it make things like adding content or a mini application to a page much easier
Here you go
1) File catalog/admin/includes/application_top.php
Add to the bottom of the file, before the last closing ?> following
// hook require(DIR_FS_CATALOG . 'includes/classes/hooks.php'); $OSCOM_Hooks = new hooks('admin');
2) File catalog/includes/application_top.php
Add to the bottom of the file, before the last closing ?> following
// hook require(DIR_FS_CATALOG . 'includes/classes/hooks.php'); $OSCOM_Hooks = new hooks('shop');
3) Upload file hooks.php
Create a file named hooks.php, copy paste the code shown below and upload it to location catalog/includes/classes/
<?php /* $Id$ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2014 osCommerce Released under the GNU General Public License */ class hooks { var $_site; var $_hooks = array(); function hooks($site) { $this->_site = basename($site); $this->register('global'); } function register($group) { $group = basename($group); $directory = DIR_FS_CATALOG . 'includes/hooks/' . $this->_site . '/' . $group; if ( file_exists($directory) ) { if ( $dir = @dir($directory) ) { while ( $file = $dir->read() ) { if ( !is_dir($directory . '/' . $file) ) { if ( substr($file, strrpos($file, '.')) == '.php' ) { $code = substr($file, 0, strrpos($file, '.')); $class = 'hook_' . $this->_site . '_' . $group . '_' . $code; include($directory . '/' . $file); $GLOBALS[$class] = new $class(); foreach ( get_class_methods($GLOBALS[$class]) as $method ) { if ( substr($method, 0, 7) == 'listen_' ) { $this->_hooks[$this->_site][$group][substr($method, 7)][] = $code; } } } } } $dir->close(); } } } function call($group, $action) { $result = ''; foreach ( $this->_hooks[$this->_site][$group][$action] as $hook ) { $result .= call_user_func(array($GLOBALS['hook_' . $this->_site . '_' . $group . '_' . $hook], 'listen_' . $action)); } if ( !empty($result) ) { return $result; } } } ?>
4) Create new folders
First a folder named /hooks/ within catalog/includes/ Then, create 2 new folders named /shop/ and /admin/ within that new folder /hooks/
Your folder structure should look like this now – the image is showing only the new folders of course
That’s all, now you have the hook system installed and can start creating hooks.
I’ll post some examples about how to create and use that “hooks” soon
George did you ever post any examples on how to use the hook system?
Dan
Hi Dan
Good that you remember me to this.
The hook system is really great and I’m implementing it already in the next MTS version
I have a couple of simple examples and will post them soon
Hi George….I was just looking at hooks again and by chance I found this thread once more…I had forgotten about it and didn’t realize that you replied. Not sure how I missed that. In any case if you are able to put up an example or two I think that would help to get us shop owners learning about and using hooks. I would be happy to post about it on the forums. Maybe we can start a thread there and provide a link to this helpful post.
Dan