I took the code from a StackOverflow post to build a module to accept only certain coupons for customers with a certain email domain for Magento v 1.0.4.1 (Yes, I know it is old but we cannot upgrade due to core modifications by a contractor so do not ask)
This is the code (located in /app/code/local/NA/LimitCoupon/Module):
<?phpclass NA_LimitCoupon_Model_Observer { protected $_domainsAllowed = array(array('COUPON1', 'email1.com'), array('COUPON2', 'email2.com') ); public function validate(Varien_Event_Observer $observer) {$timestamp = date("m-d-Y H:i:s");$msg = "Timestamp:".$timestamp;$fp = fopen("/var/tmp/".__FUNCTION__.".txt", 'a');fwrite($fp, $msg . "\n");fclose($fp); try { $this->validateRestriction($observer); } catch (Mage_Core_Exception $e) { Mage::getSingleton('checkout/session')->addError($e->getMessage()); } catch (Exception $e) { Mage::getSingleton('checkout/session')->addError($this->__('Cannot apply the coupon code.')); Mage::logException($e); } } public function validateRestriction(Varien_Event_Observer $observer) { $errMsg = false; // Get the quote: $quote = $event->getQuote(); // obtain the code being used $code = $quote()->getCouponCode(); // Get the current quote email $currentEmail = $quote()->getCustomer()->getEmail(); $explodedEmail = explode('@', $currentEmail); // Get the domain from the email address $domain = array_pop($explodedEmail);$timestamp = date("m-d-Y H:i:s");$msg = "Timestamp:".$timestamp."\nCode: ".$code."\nCurrentEmail: ".$currentEmail."\nDomain: ".$domain;$fp = fopen("/var/tmp/".__FUNCTION__.".txt", 'a');fwrite($fp, $msg . "\n");fclose($fp); switch($code) { case 'COUPON1': // check if the email address is in the email1 domain if ($domain !== $this->_domainsAllowed[$code]) { // Wrong domain $errMsg = 'This coupon code is restricted to customers of EMAIL1'; } break; case 'COUPON2': // check if the email address is in the email2 domain if ($domain !== $this->_domainsAllowed[$code]) { // Wrong domain $errMsg = 'This coupon code is restricted to customers of EMAIL2'; } break; } // handle errors if ($errMsg) { Mage::getSingleton('checkout/session')->addError($errMsg); $quote->setCouponCode(''); $quote->collectTotals()->save(); } }
The issue is, it never goes into the code at all, hence the lack of anything written to the /var/tmp directory as it should be from my FWRITE commands. What am I doing wrong here?
Essentially, I want to limit usage of COUPON1 to customers with email1.com in their email address and usage of COUPON2 to customers with email2.com in their email address.
XMLE-CommerceMagentoPHP
Last Comment
Eddie Shipman
8/22/2022 - Mon
Randy Downs
Getting any errors? Enough disk space?
Eddie Shipman
ASKER
No errors; yes, enough disk space. I believe that the issue may be in the XML files, do they look correct to you?
No it doesn't. For some reason, I believe that the module isn't being loaded. It doesn't show in my System->Configuration->Advanced->Advanced->Disable modules output list at all.
This means that the XML file in /app/code/etc/modules must be wrong.
error in XML (layout.xml or config.xml or any xml file) if you use below code in the controller action which is being called. The browser will display WHOLE XML code and if it encounters any error in it, simply gives where is the error in the XML tree.