In Magento, what is an observer exactly? What does it do? - magento

I'm an extremely novice programmer who finds himself tasked with learning how to program for Magento. So please forgive me for such a rudimentary question but there don't seem to be a ton of beginning level content on Google regarding Mage.
Can someone explain to me what exactly an observer is? What does it do? What can it be used for?
If someone can give me a super 101 explanation (not assuming much prior knowledge) you'll be my new hero. Thanks.

You can consider Event observer as a trigger.
Once you have set an event observer, for example you can set observer before or after an event, i.e., You can add a event which would execute right after user adds a product to cart or before the add product to cart.
In this event observer, you can write code to customize the data which is either passed to the occuring event (before) or is the output of occured event (after)
Reference : http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
Event :
In Magento you may consider a Controller Action as an event, for example addAction in CartController is an event.
Observer :
As mentioned in name, the observer observes when this action occurs (in our case addAction in CartController) and calls a function either before or after this addAction is called.
You may add your custom code in this obeserver for customization.

An Observer is the piece of code you'll need to write if you implement an Event.
Your question is : what is an event ?
See an event as a "broadcast action" that you can intercept in order to add your specific code to a specific action.
There are events fired all over magento key functionnalities.. For example, you can intercept:
- after or before saving a product
- product added to the cart
- etc
It's just an open-door leaved by magento core developers for you to plug-in..
In magento you have several ways to modify the behavior of the standard fucntionnality :
- you can rewrite classes ( tags in config.xml)
- and you can use the events when an event is available for the functionnality you want to modify
To understand, dive in the code and search "dispatchEvent" in app/code/core ...

Related

ODOO 8 : Why purchase receipt form doesn't have a "Pay Bill" button like supplier bill?

What I'm trying to do :
I'm trying to reproduce the button "Pay Bill" of the "Supplier Bill"'s form in the "Purchase receipt"'s form.
For that, I inherit account.voucher to add a function voucher_pay_customer, which is inspired from invoice_pay_customer. I also replicated the view account.voucher.receipt.dialog.form.
Why ?
Because the accountant currently has to create a supplier payment from an other form, in which he search for the previously create Receipt in order to register the payment
The Problem :
I can't make the writeoff_amount change in the dialog form, while trying to have as little differences between my onchanges and thoses of the invoice, since I don't want to alter some behavior I'm not aware about.
Since I'm new to the accounting module of Odoo, I'm struggling to understand the way it is developped. Hence my question : Why purchase receipt's form doesn't already have a "Pay Bill" button like supplier bill's form ? I hope that understanding the limits that were encountered during the development, or maybe simply the reason of this choice, if it is one, will help me to better understand the root of my problem, and correct it.

How to modify or remove addSuccess message in Magento?

For example, the following action will add a message which will be inserted into the page:
-When addtocart button is clicked, it will display a message saying the product was added successful or not.
The code is in app/code/core/Mage/Checkout/controllers/CartControllers.php
$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
$this->_getSession()->addSuccess($message);
-When a product review is submitted, there will be a message saying the review has been accepted for moderation.
The code is in app/code/core/Mage/Review/controllers/ProductController.php
$session->addSuccess($this->__('Your review has been accepted for moderation.'));
The above two are just examples, there are more other similar messages which are display on certain actions.
I know how to override it, change or remove the message. For the addtocart message, it can also be turned off by going to the Admin Panel.
I believe there is a better way to modify those messages than create a module and overriding the function just for modifying the message or remove it.
Does anyone know any better ways to modify or remove those addSuccess messages?
How can we modify or remove those messages after addSuccess() function is already called and the messages are added?
Of course there's a better way ;D
Take a look into your app/locale/en_US folder (or whichever language you want to edit). There you'll find a series of CSV files with translations.
Every time you see echo $this->_('Something Here'); it means there is a translation in these CSV files. This depends on the current namespace, so for Checkout messages, you'd want to look in Mage_Checkout.csv first.
Open with your favorite text editor and look for something like this:
%s was added to your shopping cart.,%s was added to your shopping cart.
Now, change the line AFTER the comma to what you would like it to be:
%s was added to your shopping cart.,We just added %s to your cart!
Alternatively, you can make it blank by just removing everything after the comma:
%s was added to your shopping cart.,
The %s denotes the variable used, which is passed as the second parameter in the _() function.
This should remove all success or error messages from your session.
$this->_getSession()->getMessages(true);
More specifically, you can use the following to remove messages from core and customer session respectively:
Mage::getSingleton('core/session')->getMessages(true);
Mage::getSingleton('customer/session')->getMessages(true);
For the addtocart message, it can also be turned off by going to the Admin Panel.
As #s-hunter said the above quote, can any one tell where to find the setting to turn it off.

Magento website popup from a observer

I have an observer listening to the controller_action_predispatch event. This is because I'm doing some geo ip related logic for each page customers are visiting.
I want to show a light-box popup to get customer's input as a part of the logic.
Part of the issue that I'm facing here is at this stage layouts are not loaded. :(
Is this possible to do? If so whats the best efficient way of doing it?
Look at
Mage_Core_Controller_Varien_Action::renderLayout()
Mage::dispatchEvent('controller_action_layout_render_before');
Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName());
That event might work better
So I had to go with a different event to access the layouts from my Observer. Best one for me was listening to controller_action_layout_generate_blocks_after event. Then I grab a block,
$myblock = $observer->getLayout()->getblock('myblock');
$myblock->setMyvar('PassMyValue');
And then use that value to popup my light box to get the user input.

Magento How can i change request parameters on the fly?

I tried to write an Observer like controller_action_predispatch and like for the default sort in Magento: store.com/category.html?dir=asc&order=name
in the observer i tried to manipulate the parameters but no succes. I tried for examples always for DESC order so i did like this: Mage::app()->getRequest()->setParam('dir', 'desc');
But after render the pruduct list it dont work... I sense i have no power on request parameters or is there a way to change them before loading a page and using a Magento event?
if you have an issue in product sorting then i will suggest to use Observer "catalog_block_product_list_collection" on this event and then observer class you can use
$observer->getEvent()->getCollection()->addAttributeToSort('price', 'ASC');
Thanks

How to call a certain method in each action method of all controller classes in Magento?

For a Magento shop I'm working on I have to check certain session variables on each load of a page. When the variables don't have the expected values I need to redirect to certain page.
No I wonder how I could implement such a behavior. Normally I would do the check in action methods of each controller, but I don't want to rewrite each controller or all their base classes.
Is there a easier way?
Magento's event architecture to the rescue! Observe the controller_action_predispatch method.
Edit: Note that this event is dispatched in both adminhtml and frontend, so Sergy's answer is important - configure the event observer under the appropriate area.
Yes,
you can always use magento events in this case:
1. controller_action_postdispatch.
2. controller_action_predispatch .
Be careful: same events are used in admin area also.

Resources