I have a custom admin config field type in Magento and I would like to load up a CSS file for that config field(also javascript too).
The field class:
class Company_Module_Block_Configure extends Mage_Adminhtml_Block_System_Config_Form_Field
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
// I would like to load the CSS and Javascripts files to the head.
return "Here comes the field output";
}
}
I have tried this, but no luck. Maybe the head rendered sooner:
Mage::app()->getLayout()->getBlock('head')->addCss('My css url...');
Or somehow I should Observe some kind of "before_body_end" event. Is it possible?
Update #1:
I have started to go through the before_body_end event:
Config.xml
<config>
<global>
<events>
<core_block_abstract_to_html_before>
<observers>
<test>
<class>Company_Module_Model_Observer</class>
<method>test</method>
</test>
</observers>
</core_block_abstract_to_html_before>
</events>
</global>
</config>
And the observed method looks like this:
public function test($block){
if($block->getEvent ()->getBlock()->getNameInLayout() == 'before_body_end'){
}
}
Related
Once an order is placed in my magento store I have a custom module so that i can save the order number into an external database table.
My custom module setup is:
Custom/ExternalOms/config.xml
So i hook into the sales_order_place_after event
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_ExternalOms>
<version>0.0.1</version>
</Custom_ExternalOms>
</modules>
<global>
<models>
<custom_externaloms>
<class>Custom_ExternalOms_ExternalOms</class>
</custom_externaloms>
</models>
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>Custom_ExternalOms_Model_ExternalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Custom/ExternalOms/Model/ExternalOms.php
and run my function:
class Custom_ExternalOms_Model_ExternalOms extends Mage_Core_Model_Abstract
{
public function exportToOMS()
{
$_order_number = Mage::getSingleton('checkout/session')->getLastRealOrderId();
// remaining code..
}
}
The code runs once the order has been placed correctly but this:
Mage::getSingleton('checkout/session')->getLastRealOrderId();
Is coming up empty
Replace your event by this code
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>externaloms/externalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
ExternalOms file by this
class Custom_Externaloms_Model_ExternalOms {
public function exportToOMS($observer)
{
$observer->getOrder();
// remaining code..
}
}
/* in case $observer->getOrder(); will not work than use
Mage::getSingleton('checkout/session')->getLastOrderId(); for getting last order id and load order
*/
I hope it will work
I'm new on magento and trying to write little extension for it (magento 1.7).
I have created tab in customer->edit, it prints multiselect, thats ok, the problem is that i cant get in observer file to catch multiselect options before saving and save them to my custom table in database.
there is some code snippets:
app/code/local/Gone/Brands/etc/config.xml
<adminhtml>
<layout>
<updates>
<customertab>
<file>customertab.xml</file>
</customertab>
</updates>
</layout>
<events>
<customer_save_after>
<observers>
<brands_hide_manufacturers>
<type>model</type>
<class>Gone_Brands_Model_Observer</class>
<method>saveHideManufacturers</method>
</brands_hide_manufacturers>
</observers>
</customer_save_after>
</events>
</adminhtml>
app/code/local/Gone/Brands/Model/Observer.php
<?php
class Gone_Brands_Model_Observer
{
public function _construct()
{
echo "ssssssssssssssssssssssssssss";
echo "<script>alert('aaa');</script>";
}
public function saveHideManufacturers() {
echo "ssssssssssssssssssssssssssss";
echo "<script>alert('bbbb');</script>";
}
}
Maye there are other solutions how to catch form from customer->edit -> my created tab with custom field?
Thank you.
==================================================================================
Finally, four hours spent for this. Maybe this helps for someone else.
config.xml
customer_save_after -> change to -> adminhtml_customer_save_after
Now works.
Finally, four hours spent for this. Maybe this helps for someone else.
config.xml
customer_save_after -> change to -> adminhtml_customer_save_after
Now works.
In magento you doesn't link to a class with its full class name.. you must use the framework :
replace :
<class>Gone_Brands_Model_Observer</class>
by :
<class>gonebrands/observer</class>
"gonebrands" (or whatever) refers to your XML node defining your MODEL layer in your config.xml
Event definition must be inside "global" tag. Also
you don't need a _construct method in your observer class
<global>
<events>
<customer_save_after>
<observers>
<brands_hide_manufacturers>
<type>singleton</type>
<class>Gone_Brands_Model_Observer</class>
<method>saveHideManufacturers</method>
</brands_hide_manufacturers>
</observers>
</customer_save_after>
</events>
</global>
I'm creating a Magento module and I want the Observer to trigger when my custom orderstatus is chosen.
Practical situation:
People select: 'Payment Received' the //DO STUFF in the Observer is triggered.
This ain't working, so what is wrong here? (the status shows up correctly though) :-(
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MOD_PaidStatus>
<version>1.0.0</version>
</MOD_PaidStatus>
</modules>
<global>
<sales>
<order>
<statuses>
<payment_received translate="label"><label>Payment Received</label></payment_received>
</statuses>
<states>
<processing translate="label">
<label>Processing</label>
<statuses>
<processing default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</processing>
<pending translate="label">
<label>Pending</label>
<statuses>
<pending default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</pending>
</states>
</order>
</sales>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<PaidStatus>
<class>MOD_PaidStatus/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</PaidStatus>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
</config>
And my Observer.php looks like:
class MOD_PaidStatus_Model_Observer {
public function observeAttributeChange($observer){
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
mail('test#testcase.com', 'testcase', 'foo bar?');
}
}
}
In order to be able to execute some actions on changing the order's attribute "status" it is preferable to observe an event. In this case the event would be sales_order_resource_save_attribute_after. The observer function could be set up as follows:
class YourPackage_YourModule_Model_Observer {
public function observeAttributeChange($observer)
{
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
// DO STUFF
}
}
In your config.xml enter the necessary observer configuration
<global>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<your_observer_node_name>
<class>yourpackage_yourmodule/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</your_observer_node_name>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
You can do this with rewriting or observer.
You do a kind of mix of these 2 concepts.
To use rewrite, do not name your file Observer, use something like Order.php :
class MOD_PaidStatus_Model_**Order** extends Mage_Sales_Model_Order
and in your config.xml, in the global node, add this :
<models>
<sales>
<rewrite>
<order>MOD_PaidStatus_Model_Order</order>
</rewrite>
</sales>
</models>
If you want to go through an Observer, use the name Observer.php and the classname should be :
MOD_PaidStatus_Model_Observer
and it shouldn't extends anything.
The function is more like
public function myEventHandling($event)
{
$order = $event->getOrder();
$status = $order->getStatus();
//DO YOUR STUFF here
}
But your should listen for a order_status_changed_after event that doesn't exist, so you add to trigger it too... Very big stuff if you're not a confirmed developer.
If you want something like this, you need an event node in your config.xml file to associate your Observer and its method to the event. Make some search about event handling in Magento
Regards,
Edit.: for observer and event way, look the previous post. But handling such a generic event is a very resource consuming way.
Im a newbie at Magento. Im trying to build a module which dynamically inserts code xml into layout xml before its rendered -
something similar to how CMS>pages have been build.
Just like how we can specify layout xml in the design section of a page (admin > cms > page), I would like to insert into layout.xml through my module.
Basically, I would like to
have an admin section from where I can enter layout code through a
form and store in database - I have figured out this part
have Magento pull these pieces of code stored in the database and create an xml file before the layout files are aggregated and interpreted. - Im unable to build this portion.
Any help would be appreciated.
Just a little enlightment
You can add those layout xml by using observer,
Let's say you want those layout xml added before the xml is generated
We can use event controller_action_layout_generate_xml_before
Here's the sample code (in config.xml)
<frontend>
<events>
<controller_action_layout_generate_xml_before>
<observers>
<add_new_layout>
<class>test/observer</class>
<method>addNewLayout</method>
</add_new_layout>
</observers>
</controller_action_layout_generate_xml_before>
</events>
</frontend>
Here's the Observer.php
public function addNewLayout($observer){
$layout = $observer->getEvent()->getLayout();
$update = $layout->getUpdate();
//$action = $observer->getEvent()->getAction();
//$fullActionName = $action->getFullActionName();
//in case you're going to add some conditional (apply these new layout xml on these action or other things, you can modify it by yourself)
//here is the pieces of layout xml you're going to load (you get it from database)
$xml = "<reference name='root'><remove name='footer'></remove></reference>";
$update->addUpdate($xml);
return;
}
Another posibility is to use the core_layout_update_updates_get_after-event and a placeholder (non-existing) layout xml:
<frontend>
<layout>
<updates>
<foobar>
<file>foo/bar.xml</file>
</foobar>
</updates>
</layout>
<events>
<core_layout_update_updates_get_after>
<observers>
<foobar>
<type>singleton</type>
<class>foobar/observer</class>
<method>coreLayoutUpdateUpdatesGetAfter</method>
</foobar>
</observers>
</core_layout_update_updates_get_after>
</events>
</frontend>
Example PHP in your observer:
/**
* Event dispatched when loading the layout
* #param Varien_Event_Observer $observer
*/
public function coreLayoutUpdateUpdatesGetAfter($observer)
{
/** #var Mage_Core_Model_Config_Element $updates */
if (Mage::getStoreConfig('foobar/general/enabled')) {
$file = Mage::getStoreConfig('foobar/general/layout_xml');
if (!empty($file)) {
$updates = $observer->getUpdates();
if ($updates->foobar) {
$updates->foobar->file = $file;
}
}
}
}
I am trying to add Observer to the Product status update event on backend. But it does not triggering the event.
<?xml version="1.0"?>
<config>
<modules>
<Mage4u_Customredirect>
<version>0.1.0</version>
</Mage4u_Customredirect>
</modules>
<global>
<events>
<catalog_product_status_update>
<observers>
<Mage4u_Customredirect_Catalog_product>
<type>singleton</type>
<class>mage4u_customredirect/observer</class> <method>on_catalog_product_status_update</method> </Mage4u_Customredirect_Catalog_product>
</observers>
</catalog_product_status_update>
</events>
</global>
</config>
And the observer function is to receive the status of the product that was updated and based on that i need to update it in another server.
<?php
class Mage4u_Customredirect_Model_Observer
{
public function on_catalog_product_status_update(Varien_Event_Observer $observer)
{
Mage::log("test " ,null,"test");
var_dump($observer);die();
}
}
?>
But it does not triggering this event. Please can someone tel me why its not working.
You should use the same notation here for class name as you use in Mage::getModel() factory method, e.g.: <class>mage4u_customredirect/observer</class>. Do not forget delete cache after this change to make it work.