Catching Magento events with a Joomla! plugin
MageBridge integrates Magento into Joomla! and besides the core functionality of MageBridge, you also have the opportunity to extend this behaviour. This is possible by writing Joomla! plugins following the regular Joomla! coding standards. If you are not familiar with writing Joomla! plugins, check the Joomla! Documentation project for more information.
What is possible?
Diving into MageBridge programming is not that straightforward. You need to have a good understanding of what the bridge can do and how it functions. The MageBridge core is able to forward requests to Magento, and fetch the result from Magento. You can add your own request to Magento by calling the MageBridge API directly, or by merging your request with other bridge-requests (modules, components) so that the Magento application is only initialized once. This tutorial is not about this functionality.
Magento is also able to generate events without Joomla! initializing them. In this case, the Magento-part of MageBridge will forward these Magento events back through to the bridge and generate corresponding Joomla! evens - we call this concept event forwarding. In this tutorial, this concept is put into practice.
Plugins in the group magento
Event forwarding from Magento to Joomla! is implemented in Joomla! through plugins of the type magento. By default, there is only one Joomla! plugin in that group - the Magento - MageBridge plugin (plugins/magento/magebridge.php) - while the filesystem also lists an plugins/magento/example.php plugin for reference.
You can create a new plugin Foobar with the following outline:
jimport( 'joomla.plugin.plugin' ); class plgMagentoFoobar extends JPlugin { }
Example of event forwarding
Within this plugin-class, public methods are defined to catch certain events. All events forwarded from Magento to Joomla! start with a prefix mage and then the original Magento event in camelcase. Take the following Magento event, which is generated every time a Magento product is created or modified:
catalog_product_save_after
By default, MageBridge does not forward this event to Joomla!, because this would downgrade your performance. But in the MageBridge settings within the Magento Admin Panel you can enable forwarding of this event. On the Joomla! side, your new plugin-class should define the following
public function mageCatalogProductSaveAfter($arguments = array()) {}
To check which arguments are available, just debug the $arguments array with something like the following:
file_put_contents('/tmp/debug.log', var_export($arguments, true), FILE_APPEND);
Not all events are forwarded
It should be noted that not all Magento events are forwarded through the bridge. Magento counts hundreds of events - actually almost every database-object already generates at least 4 events. Because of this, MageBridge already forwards a specific set of events. This set is defined in the Magento-module of MageBridge (Listener.php), where also the $arguments to a specific event are prepared.
If you need more arguments for a specific event, or you need a specific event added to the MageBridge-functionality, please post your request on the forum. We are looking forward to improve MageBridge this way.
Dangerous events
Some events are listed in MageBridge to be forwarded, but once enabled, it might make your site deadslow - simply because the events are thrown so often. Our recommendation is not to enable forwarding for the events sales_quote_save_after and checkout_allow_guest.
Created on Friday, 28 August 2009Modified on Friday, 11 March 2011
More tutorials in this section
- MageBridge plugin events
- MageBridge from the command-line
- MageBridge FAQ: Development
- Reusing MageBridge parameters in Joomla! extensions
- Catching Magento events with a Joomla! plugin
- Fetch your own Magento data from MageBridge
- Handling events with MageBridge (in progress)
- How can I create a language-pack for MageBridge?
- Creating custom store connectors
- Discovering the MageBridge Magento package
