1

How can I write an observer that sends email which tracks an eav_attribute yes or no value?

The email get's sent, if value is set to yes.

I already created the attribute and the email template.

I only need help with the observer that sends email if the attribute is set to yes on the admin page for a particular user.

Thanks.

1
  • You want to send an email after the value of the attribute is set to "Yes", is that a customer attribute or product, category? – Alan Zavagli Dec 29 '20 at 15:27
3

If you want to send an email on data change of that attribute you can build an observer on the customer_save_after Event, were you want to check the old value and the newest one of the customer and if that changed from NO to YES since this will be triggered every time you'll save the customer.

config.xml

<events>
     <customer_save_after>
         <observers>
            <vendor_module_customer_save_after>
                 <type>singleton</type>
                 <class>Vendor_Module_Model_Observer</class>
                 <method>send</method>
            </vendor_module_customer_save_after>
          </observers>
      </customer_save_after>
</events>

Your observer with the send function inside:

function send($observer){

        $customer = $observer->getEvent()->getCustomer();

        $old = $customer->getOrigData('custom_attribute');
        $new = $customer->getData('custom_attribute');
        
        if($old == 0 && $new == 1){
    
    $emailTemplate = Mage::getModel('core/email_template');
    $emailTemplate->loadDefault('template_name');
    
    $emailTemplateVariables = array();
    $emailTemplateVariables['name'] = $customer->getName();
    $emailTemplateVariables['email'] = $customer->getEmail();
    
    $senderName = Mage::getStoreConfig('trans_email/ident_general/name');
    $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');
    
    $emailTemplate->setSenderName($senderName);
    $emailTemplate->setSenderEmail($senderEmail);
    
    $emailTemplate->send($senderEmail, $senderName, $emailTemplateVariables);
    
            // Do your stuff and call the email function
        } 
}

Not sure about the part of the email send, but this should do the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.