Solved

Magento Module issue

Posted on 2016-09-20
8
37 Views
Last Modified: 2016-09-26
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):
 
<?php
class 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();
        }
    }
 

Open in new window

And the module XML file (located in /app/etc/modules):
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <NA_LimitCoupon>
            <active>true</active>
            <codePool>local</codePool>
        </NA_LimitCoupon>
    </modules>
</config>

Open in new window

And the config.xml (located in /app/code/local/NA/LimitCoupon/etc):
 <?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>
 

Open in new window


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.
0
Comment
Question by:EddieShipman
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 41808819
Getting any errors? Enough disk space?
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 41809390
No errors; yes, enough disk space. I believe that the issue may be in the XML files, do they look correct to you?
0
 
LVL 29

Assisted Solution

by:Randy Downs
Randy Downs earned 500 total points
ID: 41809426
XML seems OK. I added XML to your categories, maybe someone else will review.  Does it write to the file if you change this to false?

<NA_LimitCoupon>
            <active>true</active>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 26

Author Comment

by:EddieShipman
ID: 41809458
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.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 0 total points
ID: 41809465
When I looked at the XML file again, it wasn't like I posted above, it looked like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <LimitCoupon>
            <active>true</active>
            <codePool>local</codePool>
        </LimitCoupon>
    </modules>
</config>

Open in new window


I have changed it to this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <NA_LimitCoupon>
            <active>true</active>
            <codePool>local</codePool>
            <self_name>NA Limit Coupon</self_name>
        </NA_LimitCoupon>
    </modules>
</config>

Open in new window


And it has begun to work, except I'm getting an error:
Call to a member function getQuote() on a non-object in /app/code/local/NA/LimitCoupon/Model/Observer.php on line 82
(It's actually on line 40:
$quote = $event->getQuote();

Open in new window

0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 41809468
Maybe you can debug xml like this


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.

header("Content-Type: text/xml");
echo Mage::app()->getConfig()->getNode()->asXml();exit;

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 41809480
I have another error in the module code that I'm tracking down now. It is running now but not logging anything.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 41815743
Self answered after hint from Randy...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Could you point a way on how to connect to a remote DB from a MySQL stored function? 3 45
Wordpress Security 29 48
Help with PHP 13 27
check mysql insert 12 26
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question