Zen Cart Logo
Forums / All Other Contributions/Addons / Disallowing certain coupon codes for orders that will be giving affiliate commission

Disallowing certain coupon codes for orders that will be giving affiliate commission

Views: 460

Results 1 to 4 of 4
4 May 2015, 3:45 PM
#1
shopville avatar

shopville

Zen Follower

Join Date:
Aug 2012
Posts:
331
Plugin Contributions:
0

Disallowing certain coupon codes for orders that will be giving affiliate commission

Looking for help adding an observer to limit certain coupon codes should not work if the order is an affiliate order?
TIA

4 May 2015, 5:30 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Disallowing certain coupon codes for orders that will be giving affiliate commission

You'll need two files to accomplish this. First, /includes/auto_loaders/config.limit_coupons.php:

<?php                 
  $autoLoadConfig[200][] = array('autoType'=>'class',
                                 'loadFile'=>'observers/class.limit_coupons_observer.php');
  $autoLoadConfig[200][] = array('autoType'=>'classInstantiate',
                                 'className'=>'limit_coupons_observer',
                                 'objectName'=>'limit_coupons_observer');

That file instructs the Zen Cart base code to load the actual processing code (/includes/classes/observers/class.limit_coupons_observer.php):

<?php
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

define ('NO_COUPONS_FOR_AFFILIATE_PURCHASES', 'list,of,coupon,codes,separated,by,commas');

class limit_coupons_observer extends base {

  function __construct() {
    $this->coupons_limited = explode (',', NO_COUPONS_FOR_AFFILIATE_PURCHASES);
    $this->attach ($this, array ( 'NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION' ));

  }
  
  function update (&$class, $eventID, $p1) {
    global $messageStack, $ot_coupon;
    if (isset ($_SESSION['referrer_key']) && zen_not_null ($_SESSION['referrer_key']) && isset ($_POST['dc_redeem_code']) && in_array ($_POST['dc_redeem_code'], $this->coupons_limited)) {
      $messageStack->add_session ('redemptions', TEXT_INVALID_REDEEM_COUPON, 'caution');
      $ot_coupon->clear_posts ();
      zen_redirect (zen_href_link (FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false));
      
    }
  }
 
}

This code processes at the very end of the pre-checking code for the checkout_confirmation page (which is the first place a coupon code is inspected). Essentially, the processing checks for the 'referrer_key' which indicates that any order will be a candidate for a SNAP Affiliate's commission. If the order qualifies for a commission, the next check is to see if the coupon-code is one of the "not allowed" ones and, if so, a message indicating that the coupon code is invalid is recorded and the customer is sent back to the checkout_payment page.

4 May 2015, 6:02 PM
#3
shopville avatar

shopville

Zen Follower

Join Date:
Aug 2012
Posts:
331
Plugin Contributions:
0

Re: Disallowing certain coupon codes for orders that will be giving affiliate commission

Thanks worked like a charm!

Could you also please share how I would modify the code the disallow all coupons?

Thanks so much!

4 May 2015, 7:45 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Disallowing certain coupon codes for orders that will be giving affiliate commission

Different problem statement => different solution. You don't need either of the above two files, just the following (/includes/modules/pages/checkout_payment/header_php_no_coupons_for_affiliates.php):

<?php
if (isset ($_SESSION['referrer_key']) && zen_not_null ($_SESSION['referrer_key'])) {
  for ($i = 0, $n = count ($order_total_modules->modules); $i < $n; $i++) {
    if ($order_total_modules->modules[$i] == 'ot_coupon') {
      unset ($order_total_modules->modules[$i]);
    }
  }
  $order_total_modules->modules = array_values ($order_total_modules->modules);
}

This code checks to see if an affiliate commission is going to be applied to the order and, if so, totally removes the Coupon processing (so you don't even see the coupon-entry block).