I have two observer file. One event is called for salesrule_validator_process and other observer file is called for sales_quote_collect_totals_after. How to get the result of salesrule_validator_process in sales_quote_collect_totals_after.
-
Both method are in same class?– Murtuza ZabuawalaSep 10, 2016 at 11:32
-
1.Test_Subtotal_Model_Observer - sales_quote_collect_totals_after 2.Test_Mrp_Model_Observer - salesrule_validator_process. Not same class.– jassiSep 10, 2016 at 11:33
-
@MurtuzaZabuawala If it has same class then what will be the solution? Give me any solution .– jassiSep 10, 2016 at 11:37
-
Please check my answer– Murtuza ZabuawalaSep 10, 2016 at 11:40
2 Answers
If the observers are in the same class, you can save the result in a property, because by default observers are instantiated as singleton. That means, the same object is used in both events:
class Stack_Example_Model_Observer
{
protected $_result;
public function validatorProcess($observer)
{
$this->_result = ...;
}
public function afterTotalsCollect($observer)
{
// here you can use $this->_result;
}
}
Otherwise, you can always use a third class as singleton. Simple example:
class Stack_Example_Model_Result extends Varien_Object
{
}
Usage:
// first observer:
Mage::getSingleton('stack_example/result')->setData('result', $result);
// second observer:
$result = Mage::getSingleton('stack_example/result')->getData('result');
This example result singleton is just a dumb data object, but you could add more meaningful methods or actually process the result there instead of in the observer classes.
-
stack_example/result - Here what 'result' stand for? I am getting fatal error Fatal error: Call to a member function getData() on boolean– jassiSep 10, 2016 at 12:12
-
"result" stands for the class name part after Model_ - please look up the basics for custom module creation and working with models. Sep 10, 2016 at 12:19
-
Solution 1: If values in different class
You can use magento registry for that
for storing registry you can do as below
Mage::register('my_need', $myvalue);
and getting value
Mage::registry('my_need');
after that unregister registry like below
Mage::unregister('my_need');
Solution 2: if it is in the same class
class1{
protected $_myvariable;
function 1(){
//set value
$this->_myvariable = $myvalue;
}
function 2(){
//fetch Value
print_r($this->_myvariable);
}
}