How To Show Discount Savings in Magento

How To Show Discount Savings in Magento

First off, let’s nail down the task that we need to complete.

We have to create a module that should output the following text line on product and catalog pages: “You have saved XXX%” (where XXX is a whole number defining the discount rate), provided that the product is set to a special price (a discount price). To complete this sort of task in our module we are going to use events. But before we continue, let’s turn our attention to 2 additional methods that let you redefine the default Magento behavior, yet which are highly undesirable to use.

The first method is based on scopes ((app/code/local, app/code/community, app/code/core)) and peculiarities of autoloader. Each scope has a definite visibility scope which allows redefining classes by simple file copying and then changing the logic of the class in accordance with your own needs. Besides, the given scopes are loaded in a specific order within the app/Mage.php file by modifying the php include_path directive:

If we had decided to use this method for our task, we would need to copy the app/code/core.Mage/Catalog/Block/Product/Price.php file into app/code/local/Mage/Catalog/Block/Product/Price.php and then add the required functionality.

The second method lets us modify the logic of the initial class but without direct copying. To understand how this works, let’s check the rewrite directive of the module’s config file. This directive is used to redefine the performance of models, blocks and helpers. In our case, we would have to redefine the performance of the class in the Mage/Catalog/Block/Product/Price.php block. The config.xml would contain the following code:

When the system is reloaded, the classes are being verified for the presence of any rewrites, and in case there are any – the system uses exactly the classes that are indicated in those rewrites.

Now let’s see how we can use events to complete the above-given task. To make it simpler, let’s divide the process into 3 steps:

  • First off, we need to define the events which our module will use.
  • Secondly, we have to get the object of the product and then create and assign a new property to it. The property should contain the number that is equal to the discount rate. The helper of the module will contain the method for discount percentage calculation.
  • Thirdly, we should create a new block which will output the information about product discount on the product and catalog pages.

Step one.

Let’s see how to choose the correct event from all events that are available in Magento. All events are generated with the help of the static method Mage::dispatchEvent:

To get the list of all events we need to put the following line at the beginning of the method (do not forget to delete later):

The core_block_abstract_to_html_after event gets output in the toHtml() method of the Mage_Core_Block_Abstract class from which Magento blocks are inherited. The method returns HTML content into blocks, while transport object is transmitted into the core_block_abstract_to_html_after event which contains HTML code of the block:

With the help of transport object, it is possible to set up the output of our custom block. Besides, we will require to use two more events of the model: catalog_product_load_after and catalog_product_collection_load_afterThese events are called after the data has been loaded into the product model.

Step two.

We need to register handlers for catalog_product_load_after and catalog_product_collection_load_after. Add the following code into the module’s config.xml file:

All events that are used by the module are described within the events node. The next thing we are going to use is the node of the event name (you may check it in the first parameter Mage::dispatchEvent). Then, inside observers we should describe our handlers: indicate the name of the handler class and the method which logic will be applied. It is considered a good practice to give the handler method the same name as the event name.

Now let’s write down the method that will calculate the discount rate from the original product price. For that we need to register a helper class in the config file:

Create a helper class and include the calculation method into it:

Add a new property to the product object:

To add a new property we are going to use the default Magento possibility that lets you dynamically receive and assign new properties to objects with the help of  _get() and  _set() methods.

The thing is that Magento models are using the functionality of the Varien_Object class. The class’s constructor receives one optional argument – an array of data which is saved into the $_data property.  This class redefines php methods, thanks to which it is possible to get access to object data.  Besides, here is implemented the interface for ArrayAccess, which allows calling objects data with the help of square brackets.

Step three.

Registering a handler for the event core_block_abstract_to_html_after:

Let’s describe in the observer class the logic of adding a block that contains the information about the product discount.

Here we create a new ‘you save’  block and register a new ‘belvg/after.phtml’ template which will output the information about the discount. The parameter $observer of the observer method is an object through which we can receive the data which is being transmitted via the dispatchEvent() method in the second parameter. In our case with the help of $block = $observer→getEvent()→getBlock() we have received a block and created another custom block inside itBasically, that is all. The task is complete. Here is the link to the module.

1 comment

Post a new comment