Handling LemonStand events
Handling LemonStand events allows you to override or extend standard functionality of the application in many ways. For example, you can extend existing LemonStand models (customers, products etc), customize visitors shopping experience, execute some code before a front-store page is displayed and do other things. The list of events is growing constantly. Please contact us if you need to handle some event which is not listed below.
Handling events
To handle LemonStand events you need to develop a simple module. The process of developing LemonStand modules is described in this article. A simplest module can have only two files - the module information class and the module version information file. Event handlers can be defined in the module information class. To subscribe to system events use the subscribeEvents() method of the module information class. For example:
public function subscribeEvents()
{
Backend::$events->addEvent('shop:onNewOrder', $this, 'on_new_order');
}
The code subscribes a module information class to the shop:onNewOrder event. When it is triggered the on_new_order method (specified in the third parameter) will be called. This method (event handler) must be defined in the module information class. There are no special rules for event handler function names. Below is an example of the event handler code:
public function on_new_order($order_id)
{
// Find the order
$order = Shop_Order::create()->find_by_id($order_id);
if ($order)
{
// Do something with the order object
}
}Invoking custom module events from standard LemonStand pages
In some cases you will need to call an event handler in your module from a standard LemonStand page. The custom event solution allows you to trigger any event on some LemonStand pages in order to handle them in your custom module. The feature is supported in all back-end controllers, but implementation for the Products and Orders controllers differs from implementation in other controllers.
To call an event from your module, trigger a standard AJAX request to the onCustomEvent handler. In the extra POST fields specify the event name you want to be invoked on the server. The event handler should accept 2 parameters - the controller object and the record identifier, which you can use for loading the object from the database. For the Products and Orders controllers the second parameter is the product and order object. Thus, on the Edit Order page the second parameter will be the Shop_Order object, and on the Edit Product page the second parameter will be the Shop_Product object. Example:
// Button on the Order Preview page, which opens a popup window
// with a content provided by the Subscriptions module
<?= backend_ctr_button('Generate subscription invoice', 'generate_subscription_invoice',
array('href'=>'#', 'onclick'=>"new PopupForm(
'onCustomEvent', {
ajaxFields: {custom_event_handler: 'subscriptions:onGenerateInvoice'}
}); return false;
")) ?>
// Event subscription in the Subscriptions module
public function subscribeEvents()
{
Backend::$events->addEvent('subscriptions:onGenerateInvoice', $this, 'generate_order_invoice');
}
function generate_order_invoice($controller, $order)
{
$controller->renderPartial(PATH_APP.'/modules/subscriptions/partials/_generate_invoice.htm');
}
List of LemonStand events
Below you will find list of events triggered by different LemonStand modules.
Core module events
Events provided by the LemonStand Core module.
- core:onSendEmail event - allows to override the email sending feature,
- onDeleteEmailTemplate event - triggered when a user tries to delete an email template.
- core:onInitialize event - this event is triggered when the system initialization finishes.
- core:onUninitialize event - this event is triggered when the script execution finishes.
- core:onProcessImage event - allow to process images with third-party image manipulation tools.
- core:onBeforeDatabaseQuery event - triggered before a SQL query is sent to the database.
- core:onAfterDatabaseQuery event - triggered after a SQL query is executed by the database.
CMS module events
Events provided by the LemonStand CMS module.
- cms:onDeletePage event - triggered when a user tries to delete a page in the Administration Area.
- cms:onDeletePartial event - triggered when a user tries to delete a partial in the Administration Area.
- cms:onDeleteTemplate event - triggered when a user tries to delete a template in the Administration Area.
- cms:onBeforeDisplay event - triggered before a front-store page is displayed.
- cms:onAfterDisplay event - triggered after a front-end page is displayed.
- cms:onApplyPageSecurity event - allows to perform security check before a page is displayed.
- cms:onDisplayPageForm event - allows to load extra CSS or JavaScript files on the Create/Edit Page page.
- cms:onExtendPageModel event - allows to add new fields in the CMS page model.
- cms:onExtendPageForm event - allows to add new fields to the Create/Edit Page form.
- cms:onGetPageFieldOptions event - allows to populate custom CMS page form drop-down, radio- and checkbox list fields with options.
- cms:onGetPageNavigationVisibility event - allows to hide a page from automatically generated navigation menus.
- cms:onExtendPagesToolbar event - allows to add new buttons to the toolbar on the CMS/Pages page.
- cms:onExtendPartialsToolbar event - allows to add new buttons to the toolbar on the CMS/Partials page.
- cms:onExtendTemplatesToolbar event - allows to add new buttons to the toolbar on the CMS/Templates page.
- cms:onBeforeHandleAjax event - triggered before AJAX requests are processed.
- cms:onAfterHandleAjax event - triggered after AJAX requests are processed.
- cms:onGetCustomerGroupId event - allows to override the result of the Cms_Controller::get_customer_group_id() method
- cms:onBeforeRoute event - allows to override the default CMS routing process.
- cms:onPageNotFound - triggered when LS is unable to find a page to display, allows to bypass LemonStand's default 404 page.
Shop module events
Events provided by the LemonStand Shop module. Some events listed below are described in the Extending existing models article.
Order events - extending order model, handling order status change, handling order information change, extending order-related user interface in the Administration Area
- shop:onExtendOrderModel event - allows to extend order model. You can add new fields to the order model in the event handler.
- shop:onExtendOrderForm event - allows to add new fields to the Create/Edit Order form in the Administration Area.
- shop:onGetOrderFieldOptions event - allows to populate custom order form drop-down, radio- and checkbox list fields with options.
- shop:onNewOrder event - triggered on new order.
- shop:onOrderBeforeCreate event - this event is triggered before new order is placed.
- shop:onOrderError event - allows to process an order error.
- shop:onOrderBeforeStatusChanged event - triggered before an order changed its status.
- shop:onOrderStatusChanged event - triggered just after an order changed its status.
- shop:onBeforeOrderInternalStatusMessageSent - triggered before sending an order status change notification.
- shop:onExtendOrderPreviewToolbar event - allows to add new buttons to the toolbar above the Order Preview form.
- shop:onExtendOrdersToolbar event - allows to add new buttons to the toolbar above the order list in the Administration Area.
- shop:onDisplayOrdersPage event - allows to load extra CSS or JavaScript files on all order-related pages in the Administration Area.
- shop:onBeforeOrderRecordCreate event - triggered bafore an order record is saved to the database.
- shop:onOrderBeforeUpdate event - triggered before an order record is updated in the database.
- shop:onOrderStockChange event - fires before LemonStand updates inventory for products of a specific order.
- shop:onOrderAfterDelete event - triggered after an order has been deleted.
- shop:onOrderMarkedDeleted event - triggered after an order has been marked as deleted.
- shop:onOrderRestored event - triggered after a previously deleted order has been restored.
- shop:onConfigureOrdersPage - triggered when the Orders page is loaded, allows to configure the Orders page.
Order item events - extending order items, processing order item updates
- shop:onExtendOrderItemModel event - allows to add new columns to the order item model.
- shop:onExtendOrderItemForm event - allows to add custom fields to the Create/Edit Order Item form.
- shop:onOrderItemAdded event - triggered after an order item is added to an order.
- shop:onOrderItemUpdated event - triggered when an order item is updated.
- shop:onOrderItemDeleted event - triggered when an item is deleted from an order.
- shop:onGetOrderItemFieldOptions event - allows to populate custom order item form drop-down, radio- and checkbox list fields with options.
- shop:onGetOrderItemDisplayDetails event - allows to display custom information about an order item in the Administration Area.
Customer events - extending the customer model and customer-related user interface in the Administration Area
- shop:onExtendCustomerModel event - allows to extend customer model. You can add new fields to the customer model in the event handler.
- shop:onExtendCustomerForm event - allows to add new fields to the Create/Edit Customer form in the Administration Area.
- shop:onExtendCustomerPreviewToolbar event - allows to add new buttons to the toolbar on the Shop/Customer/Preview page.
- shop:onDisplayCustomersPage event - allows to load extra CSS or JavaScript files on all customer-related pages in the Administration Area.
- shop:onGetCustomerFieldOptions event - allows to populate custom customer form drop-down, radio- and checkbox list fields with options.
- shop:onCustomerUpdated event - triggered after a customer object has been updated.
- shop:onCustomerCreated event - triggered after a new customer has been created.
- shop:onAuthenticateCustomer event - allows to implement custom authentication scenarios.
Product events - extending the product model, product-related user interface in the Administration Area, handling file uploads on the Product Details page on the front-end website.
- shop:onExtendProductModel event - allows extend the product model with new fields.
- shop:onExtendProductForm event - allows to add new fields to the Create/Edit Product form
- shop:onGetProductFieldOptions event - allows to populate custom product form drop-down, radio- and checkbox list fields with options.
- shop:onDisplayProductForm event - allows to load extra CSS or JavaScript files on the Create/Edit Product page.
- shop:onDisplayProductList event - allows to load extra CSS or JavaScript files on the Product List page
- shop:onExtendProductsToolbar event - allows to add new buttons to the Product List toolbar.
- shop:onBeforeProductFileAdded event - triggered before file is added to the product on the Product Details page.
- shop:onAfterProductFileAdded event - triggered after a file has been uploaded to a product on the product details page.
- shop:onPreProcessProductCustomData event - allows to update product custom data fields before product is added to the cart.
- shop:onConfigureProductsController event - allows to load extra CSS or JavaScript files on all product-related pages in the Administration Area.
- shop:onProductOutOfStock event - triggered when a product goes out of stock.
Product group events - extending the product group model, product group related user interface in the Administration Area.
- shop:onExtendCustomGroupModel event - allows to add new columns to the product group model.
- shop:onExtendCustomGroupForm event - allows to add new columns to the product group model.
- shop:onGetCustomGroupFieldOptions event - allows to populate custom product group form drop-down, radio- and checkbox list fields with options.
Extra option events - extending the product extra option model, extra option-related user interface in the Administration Area.
- shop:onExtendExtraOptionModel event - allows to add new columns to the product extra option model.
- shop:onExtendExtraOptionForm event - allows to add custom fields to the Create/Edit Extra Option form.
- shop:onGetExtraOptionFieldOptions event - allows to populate custom extra option form drop-down, radio- and checkbox list fields with options.
- shop:onGetProductExtraPrice - allows to overload a product extra's price
Product option events - extending the product option model, option-related user interface in the Administration Area.
- shop:onExtendOptionModel event - allows to add new columns to the product option model.
- shop:onExtendOptionForm event - allows to add custom fields to the Create/Edit Option form.
- shop:onGetOptionFieldOptions event - allows to populate custom option form drop-down, radio- and checkbox list fields with options.
Product review events -extending the product review model, product review related user interface in the Administration Area .
- shop:onExtendProductReviewModel event - allows to extend the product review model. You can add new fields to the product review model in the event handler.
- shop:onExtendProductReviewForm event - allows to add new fields to the Product Review forms in the Administration Area (if implemented).
- shop:onProductReviewBeforeCreate event - triggered before a new product review record is created.
- shop:onProductReviewAfterCreate event - triggered after a new product review record is created.
Category events - extending the product category model and the category-related user interface in the Administration Area
- shop:onExtendCategoryModel event - allows extend the product category model with new fields.
- shop:onExtendCategoryForm event - allows to add new fields to the Create/Edit Category form
- shop:onGetCategoryFieldOptions event - allows to populate custom category form drop-down, radio- and checkbox list fields with options.
Manufacturer events - extending the manufacturer model and manufacturer-related user interface in the Administration Area
- shop:onExtendManufacturerModel event - allows to extend the manufacturer model
- shop:onExtendManufacturerForm event - allows to add new fields to the Create/Edit Manufacturer form.
Shopping cart item events - extending the shopping cart item model, handling shopping cart updates
- shop:onGetCartItemPrice event - allows to override a shopping cart item price.
- shop:onUpdateCartItemPrice event - allows to update a default shopping cart item price.
- shop:onBeforeAddToCart event - - this event is triggered before a product is added to the cart.
- shop:onAfterAddToCart event - triggered when a product is added to the shopping cart.
- shop:onBeforeRemoveFromCart event - triggered before an item is removed from the shopping cart.
- shop:onAfterRemoveFromCart event - is triggered after a product has been removed from the cart,
- shop:onBeforeSetCartQuantity event - triggered before a product quantity in the cart is changed.
- shop:onAfterSetCartQuantity event - triggered after a product quantity in the cart has been changed.
Invoice events - enabling the invoice support, processing invoice items
- shop:onExtendOrderInvoicesToolbar event - allows to add new buttons to the toolbar on the Invoices tab on the Order Preview page.
- shop:onNewInvoiceItemCopy event - allows to update an order item in a new invoice, when the invoice is created manually in the Administration Area.
- shop:onInvoiceSystemSupported event - allows to enable the invoice support.
- shop:onOrderSupportsInvoices event - allows to enable the invoice support for a specific order.
- shop:onAutomatedBillingSupported event - triggered when checking if automated billing is supported.
Shipping method events - altering the shipping cost, etc.
- shop:onUpdateShippingQuote event - allows to override shipping cost calculated by a shipping module.
- shop:onFilterShippingOptions event - allows to filter the shipping option list before it is displayed on the checkout pages.
- shop:onBeforeShippingQuote - allows to override shipping parameters before being sent to a shipping method.
Product search events - extending the product search
- shop:onRegisterProductSearchEvent event - allows to register custom product search extension functions.
Reporting events - extending the reports
- shop:onExtendReportFilters - allows to filter the order report and graph data by your own parameters in the Reports area.
List of undocumented events
Documentation for the following events will be added later.
Shop module
- shop:onBeforeOrdersRssExport
- shop:onExtendCustomerGroupForm
- shop:onExtendCustomerGroupModel
- shop:onExtendCustomerPreviewTabs
- shop:onExtendOrderPaymentTransactionsToolbar
- shop:onExtendOrderPreviewHeader
- shop:onExtendOrderPreviewTabs
- shop:onExtendOrderStatusForm
- shop:onExtendOrderStatusModel
- shop:onExtendProductPreviewHeader
- shop:onExtendProductPreviewTabs
- shop:onExtendProductPreviewToolbar
- shop:onExtendProductTypeForm
- shop:onExtendProductTypeModel
- shop:onGetCategoryProductSortingQuery
- shop:onGetCustomerGroupFieldOptions
- shop:onGetOrderFieldState
- shop:onGetProductFieldState
- shop:onGetProductSortColumns
- shop:onGetProductTypeFieldOptions
- shop:onOrderCopyBillingAddress
- shop:onPrepareProductListData
Core module
- core:onAfterSoftwareUpdate
- core:onAfterSoftwareUpdateRequest
- core:onBeforeArchiveCreate
- core:onBeforeArchiveRestore
- core:onBeforeDatabaseConnect
- core:onBeforeFormRecordCreate
- core:onBeforeFormRecordDelete
- core:onBeforeFormRecordUpdate
- core:onBeforeFormRender
- core:onBeforeFormRenderPreview
- core:onBeforeListExport
- core:onBeforeListRecordDisplay
- core:onBeforeSoftwareUpdate
- core:onBeforeSoftwareUpdateRequest
- core:onExtendUserForm
- core:onExtendUserModel
- core:onFileBeforeCreate
- onFrontEndLogin
- onLogin
CMS module
- cms:onEvaluateCode
- cms:onGetPageContent
- cms:onGetPageBlockContent
- cms:onGetPartialContent
- cms:onGetTemplateContent
- cms:onAfterRenderPartial
- cms:onBeforeDataExport
- cms:onBeforeDataImport
- cms:onBeforeRenderPartial
- cms:onExtendPartialForm
- cms:onExtendPartialModel
- cms:onExtendTemplateForm
- cms:onExtendTemplateModel
- cms:onGetPartialFieldOptions
- cms:onGetTemplateFieldOptions
- cms:onListPageEditorSidebarTabs
- cms:onPreparePageListData
- cms:onPreparePartialListData
- cms:onPrepareTemplateListData
Backend module
- backend:onBeforeRemoteEvent
- backend:onBeforeRenderPage
- backend:onBeforeRenderPartial
- backend:onControllerReady
Next: shop:onExtendCustomerModel event
Previous: Using module access points
Return to Developing LemonStand modules


Comments
Ben
Monday, March 21, 2011If I wanted to keep my module information file a bit tidy and wanted to move event handler to a separate class file that I initiate on demand, what's the lemonstand way to load and instantiate a separate class?
(sort of like the $this->load->library in codeigniter )
Aleksey Bobkov
Monday, March 21, 2011@Ben - there is no way to load a class automatically. We usually use a singleton class which is loaded in the subscribeEvents() function. For example:
public function subscribeEvents()
{
$engine = Subscriptions_Engine::get();
Backend::$events->addEvent('cms:onApplyPageSecurity', $engine, 'apply_page_security');
}
In the subscriptions_engine.php:
class Subscriptions_Engine
{
protected static $instance = null;
protected function __construct()
{
}
public static function get()
{
if (self::$instance)
return self::$instance;
return self::$instance = new self();
}
...
Ben
Monday, March 21, 2011@Aleksey
Thanks.
Does Lemonstand automatically load that Subscriptions_Engine class on demand when it's needed or do I need to specify somewhere that I want to load it?
Aleksey Bobkov
Monday, March 21, 2011@Ben - it creates the module instance in this line, in the subscribeEvents() method:
$engine = Subscriptions_Engine::get();
The class object is created here and its reference is passed to the addEvent() call. After that the system always uses the same object reference for all subsequent event calls.
Ben
Monday, March 21, 2011@Aleksey.
Yes, I understand the static call, my question was when is the class itself loaded (ie. source file include) by Lemonstand?
Does lemonstand automatically load up every class that's available in the module's classes folder at initialisation time?
Aleksey Bobkov
Monday, March 21, 2011@Ben - yes, LemonStand loads class files automatically. You should follow the naming convention explained in the Developing LemonStand modules article (http://lemonstandapp.com/docs/developing_lemonstand_modules/)
Ken
Tuesday, October 25, 2011Is there any event that gets fired when a product is edited, not sure if i misunderstood one up there but I didn't see it. Basically when an admin updates a product i want to call a function in one of my other controllers.
Thanks,
Ken
Aleksey Bobkov
Tuesday, October 25, 2011@Ken - there is a couple of non-docummented events which are triggered when a back-end form is saved: core:onAfterFormRecordCreate, core:onAfterFormRecordUpdate. You can find a short description in this forum post:
http://forum.lemonstandapp.com/topic/1456-fireevent-shoponafterproductfileadded-for-backend/page__view__findpost__p__7310
Add your comment
Loading form...