Throw errors in onepage checkout predispatch observers

Introduction

In Magento 1.x.x, before calling the action method of a controller class, Magento dispatch an event called controller_action_predispatch_<route name>. Using the observers for this predispatch event, we can do anything that needs to be done before actually calling the action method.

There are multiple steps available in the onepage checkout of default Magento 1.x.x. Situations may occur where we need to do something before saving a particular step. For instance, if we need to execute 10 lines of code before saving the shipping method, we have to create an observer for the controller_action_predispatch_checkout_onepage_saveShippingMethod event.

Likewise, the following events are getting dispatched in the onepage checkout page:

  • controller_action_predispatch_checkout_onepage_index
  • controller_action_predispatch_checkout_onepage_progress
  • controller_action_predispatch_checkout_onepage_saveMethod
  • controller_action_predispatch_checkout_onepage_saveBilling
  • controller_action_predispatch_checkout_onepage_saveShipping
  • controller_action_predispatch_checkout_onepage_saveShippingMethod
  • controller_action_predispatch_checkout_onepage_savePayment
  • controller_action_predispatch_checkout_onepage_saveOrder
  • controller_action_predispatch_checkout_onepage_review
  • controller_action_predispatch_checkout_onepage_success and more.

How can I throw errors from predispatch observers?

At times, we may need to throw errors and stop execution from these predispatch observers. For this purpose we can have the following private method defined inside our observers:

To use this private method, we can call this from our observer method. For example,

This will throw the error in native browser alert box and stop the execution.

Hope this will be helpful for someone. Please share this page, if it helps you. If you spot any errors, comment below.

 

Share this article

Leave a comment