I think you must be interested in how to change the action of a function core in Magento. As in the last post, you can know the way which Michael introduced Override core class. My post today provides you another method to conduct: Event Observer.

When we want to change the action of a function core in Magento, we can use two ways including: override core class and event-driven architecture. However, Override core has a weakness that each class can only override once. Regarding the event, we can run it in different modules.

I – Register an Observer

• Step 1: Register an Observer for events by the file config.xml

<(scope)>
<events>
<(eventname)>
<observers>
<(observer_identify>
<type>model</type>
<class>Magestore_FeaturedProduct_Model_Observer</class>
<method>methodName</method>
</(observer_identify)>
</observers>
</(eventname)>
</events>
</(scope)>
  • (scope): may be the global/adminhtml/frontend. It is used to determine the place to set the Observer for an event. Global is to call this observer whenever while with adminhtml, observer is only called in the admin area. And with frontend, observer is only called in frontend.
  • (eventname): the event name which the system dispatches.
  • type: model/singleton/object – the ways to call the observer.
  • class: the class name of the observer. You can use the class name (as above it is Magestore_FeaturedProduct_Model_Observer) or magento’s class name (example: featuredproduct/observer).
  • method: the client function name which is called.

• Step 2: Write a client function for the Observer

<?php
class Magestore_FeaturedProduct_Model_Obeserver
{
 public function methodName($observer){
   //$object== $observer->getEvent()->getObject();
   //$observer is the input parameter of the event
   }
}

II – Dispatch events
Dispatching events is carried out through the function dispatchEvent in class Mage. We can use Mage::dispatchEvent($eventName, array $params). For example:

Mage::dispatchEvent('custom_event', array('object'=>$this));

The client function can receive the parameter of this event by using the command: $object = $observer->getEvent()->getObject();

III – Cron job
Function: runs a function according to the schedule time.
The configuration in file config.xml

<crontab>
<jobs>
<catalogrule_appy_all>
<schedule><cron_expr>0 2***</cron_expr></schedule>
<run><model>catalogrule/observer::dailyCatalogUpdate</model></run>
</catalogrule_apply_all>
</jobs>
</crontab>

For details:

    • The tab is to set the running time. The time parameter including:

(minute) (hour) (day) (month) (year)

  • the path to the function that is run

IV – Questions

1. Which the configuration below allowing Cron Job to run every 20 minutes?

a.         20 * * *  *       b.  0,20,40 * * * *       c.  0 20 * * * *            d.  0:20 * * * *

The answer is b.

2. Could you explain the effect of the bar <args> in the event configuration?

Can you give the answer of the second question? I’ll wait for your response. Don’t hesitate to leave any suggestions here.

Thanks a lot :).

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

5 Comments

  1. Vishal Lakhani Reply

    1. Which the configuration below allowing Cron Job to run every 20 times? for this answer is wrong as per this article http://www.pantz.org/software/cron/croninfo.html

    here argument must be as below

    # Minute Hour Day of Month Month Day of Week Command
    # (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
    0 2 12 * 0,6 /usr/bin/find

    • Thanks for your question. There was a mistake in our question. It should be “every 20 minutes” instead of “every 20 times”. Sorry for confusing you.

  2. Vishal Lakhani Reply

    Thanks for reply blanka but still i have confusion as per Crontab syntax is : Minute Hour Day of Month Month Day of Week . as per this a must be answer. so can you please tell me is magento using some other things while fetching?

    • Hi Vishal Lakhani,
      I will explain to you in more details about A and B answers.
      B answer means the function runs every 1st, 20th, 40th minute of every hour. As a result, the structure must be “0,20,40****”.
      A answer means the function runs every 20th minute of every hour.
      According to the question, B must be the correct answer.
      Hope you satisfied! Have a nice day!

  3. Vishal Lakhani Reply

    Hi blanka… Now don’t have confusion.. thanks for reply…you are correct B must be answer

Write A Comment