Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict a [magento] coupon code to a specific list of customers?

Tags:

magento

Any ideas on how to restrict the coupon code to be used by a specific list of customers?

Background: My company is doing a promo, and we already emailed a discount code out to a list of 2000+ highly valuable customers. We don't want ALL of our customers to be able to use the code, just the ones in our list. And the problem is that the coupon code has already been posted on some popular forums, and other places, so unless we do something to prevent it, we'll get a bunch of orders from folks that weren't intended to get the code.

I am trying to do some emergency damage control - I figure I am gonna have to make a code modification somewhere, but I am still looking around, can't figure out the best place. I have a list of customers in a file.

like image 298
shaune Avatar asked Nov 30 '25 10:11

shaune


1 Answers

You could create an observer observing salesrule_validator_process (this event is triggered when a shopping cart rule is applied) which would have to check 2 things:

  • the rule applied is the rule you wanna restrict to your customers list:

    $ruleId = $observer->getEvent()->getRule()->getRuleId();
    
  • if the first condition is met, then check that the customer is logged in and that is email is one of the emails in your list:

    if ($observer->getEvent()->getQuote()->getCustomer()->getEmail()) {
        $customerEmail = $observer->getEvent()->getQuote()->getCustomer()->getEmail();
        if ($customerEmail IS ON YOUR LIST) {
            return $this;
        } else {
            $event = $observer->getEvent();
            $result = $event->getResult();
            $result->setBaseDiscountAmount(0)->setDiscountAmount(0);
    } else {
        $event = $observer->getEvent();
        $result = $event->getResult();
        $result->setBaseDiscountAmount(0)->setDiscountAmount(0);
    }
    

you should refactor this code to queep it DRY, and also I haven't make an actual if condition, but you get the picture.
also you'll have to change the message "coupon has been succesfully applied".
tell me if you need more details.

like image 139
OSdave Avatar answered Dec 03 '25 09:12

OSdave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!