I'm posting the solution for others to use...
This is the code (located in /app/code/local/NA/LimitCoupon/Module/Observer.php):
<?php
class NA_LimitCoupon_Model_Observer
{
protected $_domainsAllowed = array(array('COUPON1', 'email1.com'),
array('COUPON2', 'email2.com')
);
public function validate(Varien_Event_Observer $observer)
{
try
{
$this->validateRestriction($observer);
}
catch (Mage_Core_Exception $e)
{
Mage::getSingleton('checkout/session')->addError($e->getMessage());
}
catch (Exception $e)
{
Mage::getSingleton('checkout/session')->addError('Cannot apply the coupon code.');
Mage::logException($e);
}
}
public function validateRestriction(Varien_Event_Observer $observer)
{
$errMsg = false;
// obtain the code being used
$code = $observer->getEvent()->getQuote()->getCouponCode();
// Get the current quote email
$currentEmail = $observer->getEvent()->getQuote()->getCustomer()->getEmail();
$explodedEmail = explode('@', $currentEmail);
// Get the domain from the email address
$domain = array_pop($explodedEmail);
switch($code)
{
case 'COUPON1':
// check if the email address is in the EMAIL1 domain
if ($domain !== $this->_domainsAllowed[0][1])
{
// Wrong domain
$errMsg = 'This coupon code is restricted to customers with EMAIL1';
// Record this error so we may notify the customer
$timestamp = date("m-d-Y H:i:s");
$msg = "Timestamp:".$timestamp."\nCode: ".$code."\nCurrentEmail: ".$currentEmail."\nDomain: ".$domain."\n";
$fp = fopen("/var/tmp/validateRestriction.txt", 'a');
fwrite($fp, $msg . "\n");
fclose($fp);
}
break;
case 'COUPON2':
// check if the email address is in the EMAIL2 domain
if ($domain !== $this->_domainsAllowed[1][1])
{
// Wrong domain
$errMsg = 'This coupon code is restricted to customers with EMAIL2';
// Record this error so we may notify the customer
$timestamp = date("m-d-Y H:i:s");
$msg = "Timestamp:".$timestamp."\nCode: ".$code."\nCurrentEmail: ".$currentEmail."\nDomain: ".$domain."\n";
$fp = fopen("/var/tmp/validateRestriction.txt", 'a');
fwrite($fp, $msg . "\n");
fclose($fp);
}
break;
}
// handle errors
if ($errMsg)
{
Mage::getSingleton('checkout/session')->addError($errMsg);
$observer->getEvent()->getQuote()->setCouponCode('');
$observer->getEvent()->getQuote()->collectTotals()->save();
}
}
}
And the module XML file (located in /app/etc/modules/NA_LimitCoupon.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Limits coupons to certain emails using an observer
*/
-->
<config>
<modules>
<NA_LimitCoupon>
<active>true</active>
<codePool>local</codePool>
<self_name>NA Limit Coupon</self_name>
</NA_LimitCoupon>
</modules>
</config>And the config.xml (located in /app/code/local/NA/LimitCoupon/etc/config.xml):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<NA_LimitCoupon>
<version>0.0.1</version>
</NA_LimitCoupon>
</modules>
<global>
<models>
<module>
<class>NA_LimitCoupon_Model</class>
</module>
</models>
<events>
<salesrule_validator_process>
<observers>
<LimitCoupon_validate>
<type>singleton</type>
<class>module/observer</class>
<method>validate</method>
</LimitCoupon_validate>
</observers>
</salesrule_validator_process>
</events>
</global>
</config>