Make a custom observer fire before an existing Magento observer - magento

Is there a way to make an observer that you have custom made have higher priority than the Magento one that fires on the same event. My problem is I am modifying the output of the Welcome block but the PageCache for enterprise fires on the same corecore_block_abstract_to_html_after event.
I tried putting this in my config but it didn't work like cron jobs do.
<core_block_abstract_to_html_after>
<observers>
<modify_welcome>
<type>singleton</type>
<class>groupname_page/observer</class>
<method>changeWelcomeText</method>
</modify_welcome>
<enterprise_pagecache>
<class>enterprise_pagecache/observer</class>
<method>renderBlockPlaceholder</method>
</enterprise_pagecache>
</observers>
</core_block_abstract_to_html_after>

/app/etc/modules/Groupname_Page.xml
<config>
<modules>
<Groupname_Page>
<active>true</active>
<codePool>local</codePool>
<depends>
<!-- Your dependencies go here -->
</depends>
</Groupname_Page>
<Enterprise_PageCache>
<depends>
<Groupname_Page />
</depends>
</Enterprise_PageCache>
</modules>
</config>

There is no way to control order of observers.
If you take a look on the Mage_Core_Model_App::dispatchEvent() method, you'll see how Magento get all observers for a given event from config.xml files and fires them one by one.
Rewriting the Welcome block will help you.

another option is to disable the enterprise cache observer and in your own observer run the method that would have been executed by the disabled observer
In your module's config.xml:
<core_block_abstract_to_html_after>
<observers>
<modify_welcome>
<type>singleton</type>
<class>groupname_page/observer</class>
<method>changeWelcomeText</method>
</modify_welcome>
<enterprise_pagecache>
<type>disabled</type>
</enterprise_pagecache>
</observers>
</core_block_abstract_to_html_after>
And in your Observer.php:
public function changeWelcomeText(Varien_Event_Observer $observer)
{
//do the stuff you want your observer to do first
//and after it's done, call the Enterprise observer's method
Mage::getSingleton('enterprise_pagecache/observer')->renderBlockPlaceholder($observer);
}
HTH

Related

Magento - How can i run function when the checkout page is opened?

I am using Magento 1.9.2 and i am working on a custom extension.
My question is:
How can i run custom php function when the checkout page is opened?
I know i have to mess around with the events of the extension config file but i am not exactly sure how i can point the function in the observer which i want to run when the checkout page is opened.
Here is events part of my config:
<events>
<controller_action_postdispatch_opc_json_saveOrder>
<observers>
<hss_save_order_onepage>
<class>paypal/observer</class>
<method>setResponseAfterSaveOrder</method>
</hss_save_order_onepage>
</observers>
</controller_action_postdispatch_opc_json_saveOrder>
<sales_order_place_before>
<observers>
<custom_ads>
<class>opc/observer</class>
<method>checkitemsincartv</method>
</custom_ads>
</observers>
</sales_order_place_before>
<sales_order_place_after>
<observers>
<newsletter_order_place_after>
<class>opc/observer</class>
<method>newsletter</method>
</newsletter_order_place_after>
</observers>
</sales_order_place_after>
<checkout_submit_all_after>
<observers>
<opc_checkout_submit_all_after>
<class>opc/observer</class>
<method>applyComment</method>
</opc_checkout_submit_all_after>
</observers>
</checkout_submit_all_after>
</events>
Here is the function that i want to run:
public function checkitemsincartv($observer)
{
Mage::getSingleton('core/session')->setItemsInCart("Works");
$data = $observer->getEvent()->getOrder();
Mage::log($data->debug(), null, "order.log", true);
}
How can i achieve that?
Thanks in advance!
You need to use the event - controller_action_predispatch_checkout_onepage_index or controller_action_postdispatch_checkout_onepage_index All controllers inherit the preDispatch or postDispatch method which fires a generic event for predispatch/postdispatch, and a specific event based on the requested action path.
The controller you need is in the checkout module and is called onepage with a default action of index.
Checkout Mage_Core_Controller_Varien_Action::preDispatch() or Mage_Core_Controller_Varien_Action::postDispatch() to see the relevant code

How to User sales_order_place_after for Recurring Profile

I am using sales_order_place_after event in observer event
<sales_order_place_after>
<observers>
<Test_Check_Model_Observer>
<type>singleton</type>
<class>Test_Check_Model_Observer</class>
<method>SubscribePlan</method>
</Test_Check_Model_Observer>
</observers>
</sales_order_place_after>
Then My Observer.php's Method SubscribePlan Contains
public function SubscribePlan($observer) {
die('getting called');
}
This event is Working but when i Place Order for Recurring Profile this event doesn't work, Can some one please Tell me any alternate event which also works for recurring profile after placing order
Here the code
<global>
<models>
<test>
<class>Test_Check_Model</class>
</test>
</models>
<events>
<events>
<sales_order_place_after>
<observers>
<mysales_order_observer>
<type>singleton</type>
<class>test/observer</class>
<method>SubscribePlan</method>
</mysales_order_observer>
</observers>
</sales_order_place_after>
</events>
</global>
I'm looking for something similar recently and my solution is
override model Mage_Sales_Model_Recurring_Profile with your own model
Inside your own model, set property $_eventPrefix to for example "recurring_profile"
create observer for event recurring_profile_save_commit_after.
If you need exactly the event when profile is created, you may need to override submit method and either dispatch a customized event or just put all code there without using event/observer at all.
P.S. I used Magento 1.9.3.2

Magento Lost Style after checkout_onepage_controller_success_action

<events>
<checkout_onepage_controller_success_action>
<observers>
<sendmail>
<type>singleton</type>
<class>Xkey_Redegrupo_Model_Observer</class>
<method>sendMailRGroup</method>
</sendmail>
</observers>
</checkout_onepage_controller_success_action>
</events>
If I change the checkout_onepage_controller_success_action to sales_order_after_save, work but send 2 times the e-mail, when I use this, the e-mail sends succefull but the layout of /onepage/checkout/success/ lost style ? Why? ! help me Ç.Ç
Kin checkout_onepage_controller_success_action is an event is occur after order has successfully place and redirected to checkout/onepage/success pages.it an fronted event.sales_order_after_save is global event and it is run when you save order. several time are occurs during order procedure....
Exmple of event
<checkout_onepage_controller_success_action>
<observers>
<create_invoice_for_zero_payment>
<class>zerosubtotalpaymentmethod/createinvoice</class>
<method>automaticinovoice</method>
</create_invoice_for_zero_payment>
</observers>
</checkout_onepage_controller_success_action>

Magento only the last event is executed

i have a small problem. I have register two events, but only the last was alwas called,
can someone help me
<!-- Events -->
<events>
<checkout_type_onepage_save_order_after>
<observers>
<save_after>
<type>singleton</type>
<class>XXX_testr_Model_Observer_OrderObserver</class>
<method>orderSubmitEvent</method>
</save_after>
</observers>
</checkout_type_onepage_save_order_after>
<customer_save_after>
<observers>
<XXX_testr_observer_observer>
<type>singleton</type>
<class>XXX_testr_Model_Observer_Observer</class>
<method>customerSaveEvent</method>
</XXX_testr_observer_observer>
</observers>
</customer_save_after>
</events>
At first glance it could possibly be an observer name clash, try namespacing it to be unique
try changing:
save_after
to:
sdz_wawiconnector_observer_save_after
Depending on your usage, you might have more luck working with the controller thrown event checkout_onepage_controller_success_action.
Firstly, I can tell that your xml is valid.
Check such things:
duplicates of observer names (may be save_after or XXX_testr_observer_observer have already exist)
Look to the section, where you put these observers and there you want them to be executed. For example, you put them into frontend section, so customer_save_after will not run in admin.
Make a check if some other observer (from 3rd party extensions) do not crashes before. Or, for example, it can use 'die' in its code.
Check if some other extension rewrites core model/controller (or your store have such rewrite in app/code/local/Mage/.. section) and doesn't have event dispatcher in its code

How to scale image in cms Magento

When the admin adds an image to a cms page in Magento, and then resize the image:
then the image at the frontend retains its original size, but scaled.
Is there any solution to save the image in smaller size when the page is saved?
There's no way this will happen with any default magento. However, you could put together a module that will observe your cms page saved data. Take for instance this event:
cms_page_prepare_save
with parameters:
array(’page’ ⇒ $model, ‘request’ ⇒ $this→getRequest())
You can create the module starting with only 3 files:
/app/etc/Electricjesus_Cms.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Electricjesus_Cms>
<active>true</active>
<codePool>local</codePool>
</Electricjesus_Cms>
</modules>
</config>
/app/code/local/Electricjesus/Cms/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Electricjesus_Cms>
<version>0.1.0</version>
</Electricjesus_Cms>
</modules>
<global>
<models>
<cms>
<class>Electricjesus_Cms_Model</class>
</cms>
</models>
<events>
<cms_page_prepare_save>
<observers>
<prepareSave>
<class>cms/observer</class>
<method>prepareSave</method>
</prepareSave>
</observers>
</cms_page_prepare_save>
</events>
</global>
</config>
/app/code/local/Electricjesus/Cms/Model/Observer.php:
<?php
class Electricjesus_Cms_Model_Observer {
function prepareSave ( $observer ) {
$request = $observer->getRequest();
$params = $request->getParams();
// scan the request and params for stuff related with the images..
// find out scaling and pass into a good resizer like TimThumb or
// Zend_Filter_ImageSize etc.
}
}
While the above answer is not complete, this is a good place to start. I'll try and develop a complete solution whenever I have more time on it.
Alternative method:
Since I mentioned TimThumb above, you can also deploy that at your magento root and start adding your images via HTML editor instead of the usual WYSIWYG editor. If installing thumb.php on your website root directory, you can use it like so:
<img src="/thumb.php?src=images/image.jpg&w=100&h=50" alt="Hey" />
Good luck!

Resources