1

I have this code to caputre magento's mail method:

Mage::dispatchEvent('email_template_send_before', [
            'email' => $email,
            'email_to' => $name,
            'variables' => $variables,
            'template' => $this->getId()
        ]);

        $returnVal = parent::send($email, $name, $variables);

        Mage::dispatchEvent('email_template_send_after', [
            'email' => $email,
            'email_to' => $name,
            'variables' => $variables,
            'return_value' => $returnVal
        ]);

I have an observer, which is working and doing what it's needs to do. I'd like to set a value in the observer and then use that value in this method. How can I do this?

2
  • is this code, your code, or core?
    – Marius
    Dec 7, 2016 at 15:41
  • This is my code, overriding the core method (\Mage_Core_Model_Email_Template::send)
    – user46761
    Dec 8, 2016 at 7:59

1 Answer 1

4

If you want to be able to modify something in an observer you need to pass that variable to the observer as an object because objects are passed by reference and arrays are not.

So you can change this:

    Mage::dispatchEvent('email_template_send_before', [
        'email' => $email,
        'email_to' => $name,
        'variables' => $variables,
        'template' => $this->getId()
    ]);

to this:

$observerData =  [
    'email' => $email,
    'email_to' => $name,
    'variables' => $variables,
    'template' => $this->getId()
];
$object = new Varien_Object($observerData);
Mage::dispatchEvent('email_template_send_before', ['object'=>$object]);

Then, in your observer you can change the variables like this:

public function doSomething($observer)
{
    $object = $observer->getEvent()->getObject();
    $variables = $object->getVariables();
    //change the value of $variables as you want here
    //...
    //then set the changed variables again to the original object
    $object->setVariables($variables);
}

Then change the line

$returnVal = parent::send($email, $name, $variables);

to

$email = $object->getEmail();
$name = $object->getName();
$variables = $object->getVariables();
$returnVal = parent::send($email, $name, $variables);

In conclusion, your code should look like this:

$observerData =  [
    'email' => $email,
    'email_to' => $name,
    'variables' => $variables,
    'template' => $this->getId()
];
$object = new Varien_Object($observerData);
Mage::dispatchEvent('email_template_send_before', ['object'=>$object]);
$email = $object->getEmail();
$name = $object->getName();
$variables = $object->getVariables();
$returnVal = parent::send($email, $name, $variables);
$afterObserverData = [
    'email' => $email,
    'email_to' => $name,
    'variables' => $variables,
    'return_value' => $returnVal
];
$afterObserverObject = new Varien_Object($afterObserverData);
Mage::dispatchEvent('email_template_send_after', ['object' => $afterObserverObject]);   

For the last event dispatch there is no need to convert everything to an object but it might help you if you have additional code after the even dispatch that uses the same variables that are passed to the event.

1
  • This! "you need to pass that variable to the observer as an object because objects are passed by reference and arrays are not" Aug 17, 2018 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.