Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Created October 31, 2019 17:16
Show Gist options
  • Save cgi-caesar/fbdd2a2b362d8d659046944962d425c3 to your computer and use it in GitHub Desktop.
Save cgi-caesar/fbdd2a2b362d8d659046944962d425c3 to your computer and use it in GitHub Desktop.
aMember (site.php): Limit usage of coupon by IP address
<?php
Am_Di::getInstance()->hook->add(Am_Event::VALIDATE_COUPON, function(Am_Event $e) {
$batch = $e->getCouponBatch();
$coupon = $e->getCoupon();
$used = $e->getDi()->invoiceTable->countBy([
'coupon_id' => $coupon->pk(),
'remote_addr' => $_SERVER['REMOTE_ADDR']
]);
if ($used >= $batch->user_use_count) {
$e->addReturn(___('Coupon usage limit exceeded'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment