0

I have written a custom observer during customer save after event to generate unique coupon codes and append in welcome email during that is sent during Customer Registration.

The coupon code functionality is working fine. Using send() I am trying to pass the custom variables I stored in an array. But I am unable to add it in the template.

Please find below my code snippets.

Observer.php

<?php

class XYZ_Promocode_Model_Observer {
    public function customerSaveAfter(Varien_Event_Observer $o) {
        $customerData = $o -> getCustomer() -> getData();

        if (!$o -> getCustomer() -> getOrigData()) {
            $modelcollection = Mage::getModel('salesrule/rule') -> getCollection();
            $newCollection = array();
            $newCollection = $modelcollection -> getData();

            $ruleName = $newCollection[0]['name'];
            $rule_id = $newCollection[0]['rule_id'];

            if ($rule_id == 1 && $ruleName == "RegisteredUserPromocode") {
                $rule = Mage::getModel('salesrule/rule') -> load(1);

                $generator = Mage::getModel('salesrule/coupon_massgenerator');

                $parameters = array('count' => 1, 'format' => 'alphanumeric', 'dash_every_x_characters' => 4, 'prefix' => 'ABCD-EFGH-', 'suffix' => '-WXYZ', 'length' => 8);

                if (!empty($parameters['format'])) {
                    switch( strtolower($parameters['format']) ) {
                        case 'alphanumeric' :
                        case 'alphanum' :
                            $generator -> setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC);
                            break;
                        case 'alphabetical' :
                        case 'alpha' :
                            $generator -> setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL);
                            break;
                        case 'numeric' :
                        case 'num' :
                            $generator -> setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC);
                            break;
                    }
                }

                $generator -> setDash(!empty($parameters['dash_every_x_characters']) ? (int)$parameters['dash_every_x_characters'] : 0);
                $generator -> setLength(!empty($parameters['length']) ? (int)$parameters['length'] : 6);
                $generator -> setPrefix(!empty($parameters['prefix']) ? $parameters['prefix'] : '');
                $generator -> setSuffix(!empty($parameters['suffix']) ? $parameters['suffix'] : '');


                $rule -> setCouponCodeGenerator($generator);
                $rule -> setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO);


                $count = !empty($parameters['count']) ? (int)$parameters['count'] : 1;
                $codes = array();
                for ($i = 0; $i < $count; $i++) {
                    $coupon = $rule -> acquireCoupon();
                    $coupon -> setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED) -> save();

                    $code = $coupon -> getCode();
                }

                $emailTemplate = Mage::getModel('core/email_template') -> loadDefault('coupon_new_customer');

                $emailTemplateVariables = array();

                $emailTemplateVariables['username'] = $customerData['firstname'] . ' ' . $customerData['lastname'];
                $emailTemplateVariables['customer_email'] = $customerData['email'];
                $emailTemplateVariables['promo_code'] = $code;

                $result = $emailTemplate -> send($customerData['email'], $customerData['firstname'] . ' ' . $customerData['lastname'], $emailTemplateVariables);
            }
        }
    }

}

Ways tried to retrieve in email template:

1. {{var promo_code}}

2. {{var emailTemplateVariables.promo_code}}

1

And the answer is...

I need to first save the custom promo code in customer collection. Then only I can retrieve it in any email template of Magento. For that I need to override Mage/Customer/Model/Customer.php

class XYZ_Promocode_Model_Customer extends Mage_Customer_Model_Customer
{
    public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeId = '0')
    {
        $types = array(
            'registered' => self::XML_PATH_REGISTER_EMAIL_TEMPLATE, // welcome email, when confirmation is disabled
            'confirmed' => self::XML_PATH_CONFIRMED_EMAIL_TEMPLATE, // welcome email, when confirmation is enabled
            'confirmation' => self::XML_PATH_CONFIRM_EMAIL_TEMPLATE, // email with confirmation link
        );
        if (!isset($types[$type])) {
            Mage::throwException(Mage::helper('customer')->__('Wrong transactional account email type'));
        }

        if (!$storeId) {
            $storeId = $this->_getWebsiteStoreId($this->getSendemailStoreId());
        }

        /* 
         * Retrieve coupon code from customer session 
         */

        $getCouponCode = Mage::getSingleton('customer/session')->getCode();

        $this->_sendEmailTemplate($types[$type], self::XML_PATH_REGISTER_EMAIL_IDENTITY,
            array('customer' => $this, 'back_url' => $backUrl, 'promo_code' => $getCouponCode), $storeId);

        return $this;
    }
}

Here I am saving the coupon code generated in session in the observer and retrieving it in customer model.

Updated Observer file:

class XYZ_Promocode_Model_Observer
{
    public function customerSaveAfter(Varien_Event_Observer $o)
    {
        $customerData = $o->getCustomer()->getData();

        $rule = Mage::getModel('salesrule/rule')->load(3);

        $generator = Mage::getModel('salesrule/coupon_massgenerator');

        $parameters = array(
            'count' => 1,
            'format' => 'alphanumeric',
            'dash_every_x_characters' => 4,
            'prefix' => 'ABCD-EFGH-',
            'suffix' => '-WXYZ',
            'length' => 8,
        );

        if (!empty($parameters['format'])) {
            switch (strtolower($parameters['format'])) {
                case 'alphanumeric' :
                case 'alphanum' :
                    $generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC);
                    break;
                case 'alphabetical' :
                case 'alpha' :
                    $generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL);
                    break;
                case 'numeric' :
                case 'num' :
                    $generator->setFormat(Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC);
                    break;
            }
        }

        $generator->setDash(
            !empty($parameters['dash_every_x_characters']) ? (int) $parameters['dash_every_x_characters'] : 0
        );
        $generator->setLength(!empty($parameters['length']) ? (int) $parameters['length'] : 6);
        $generator->setPrefix(!empty($parameters['prefix']) ? $parameters['prefix'] : '');
        $generator->setSuffix(!empty($parameters['suffix']) ? $parameters['suffix'] : '');

        $rule->setCouponCodeGenerator($generator);
        $rule->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO);

        $count = !empty($parameters['count']) ? (int) $parameters['count'] : 1;
        $codes = array();
        for ($i = 0; $i < $count; ++$i) {
            $coupon = $rule->acquireCoupon();
            $coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->save();

            $code = $coupon->getCode();
        }

        /* 
         * Generated coupon code stored in customer session 
         */
        Mage::getSingleton('customer/session')->setCode($code);
    }
}
| improve this answer | |
1

Try with below event which is fire immediate after customer registration from frontend

customer_register_success

| improve this answer | |

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.