Member Discounts by Category (v. 2.8x)

The Site Builder system has a members coupon area that allows a store owner to create a member code which applies a straight percent off to all items in the catalog.

To modify this functionality and only allow items in certain categories to receive this discount, you can apply this modification. Note that this does require database changes!

  1. Make a backup of all files and your database.

  2. In phpMyAdmin, select the SQL tab and run the query:

    ALTER TABLE `opc_categories` ADD `AllowDiscounts` ENUM( 'Yes', 'No' ) NOT NULL DEFAULT 'Yes';

    This will add a new field to the categories table. Note that all existing categories will be discountable by default. (To change this, change DEFAULT 'Yes' to DEFAULT 'No' in the code above.)

  3. Open the openinfo.php file in the /enc/ folder.

    • Change the following code (around line 415):

      $memrow = mysql_fetch_array($memresult);
      $membername = stripslashes($memrow[GroupName]);
      $memdisc = (float)$memrow[Discount];
      $memmsg = "

      "; if ($memrow[Discount] == 0) $memmsg .= "Welcome, $membername"; else $memmsg .= "A $memdisc% discount for $membername will be applied."; $memmsg .= "

      ";

      to:

      $memrow = mysql_fetch_array($memresult);
      // CHECK CATEGORIES WITH DISCOUNTS
      $allow_coupon_cats=array();
      $allow_member_id=array();
      $couponcats=mysql_query("SELECT ID FROM " .SB_DB_PREFIX ."_categories WHERE AllowDiscounts='Yes'");
      while($coupcat=mysql_fetch_array($couponcats))
      {
      $allow_coupon_cats[]=$coupcat[ID];
      }
      if(!empty($allow_coupon_cats))
      {
      $couponcatlist=implode(',', $allow_coupon_cats);
      $couponitems=mysql_query("SELECT ID FROM " .SB_DB_PREFIX ."_items WHERE Category" .implode(" IN ($couponcatlist) OR Category", range(1,15)) ." IN ($couponcatlist)");
      while($coupitem=mysql_fetch_array($couponitems))
      {
      $allow_member_id[]=$coupitem[ID];
      }
      }
      // END CHECK
      // NO DISCOUNTS
      if(!empty($allow_member_id))
      {
      $membername = stripslashes($memrow[GroupName]);
      $memdisc = (float)$memrow[Discount];
      $memmsg = "

      "; if ($memrow[Discount] == 0) $memmsg .= "Welcome, $membername"; else $memmsg .= "A $memdisc% discount will be applied to selected items."; $memmsg .= "

      "; }
  4. Open the products_variables.php files in the /enc/ folder.

    1. Change the code (around line 175) in the var_discounts function from:

      global $label_discmsg1, $label_discmsg2, $Currency;

      to:

      global $label_discmsg1, $label_discmsg2, $Currency, $allow_member_id, $searchrow;
    2. A little further down, change:

      if ($membprice == "yes" AND (empty($membcode) OR $membdisc == 0))

      to:

      if(!in_array($searchrow[ID], $allow_member_id) OR ($membprice == "yes" AND (empty($membcode) OR $membdisc == 0)))
    3. Change the code (around line 507) in the var_member function from:

      global $membercode, $memdisc;

      to:

      global $membercode, $memdisc, $allow_member_id, $searchrow;
    4. A little further down, change:

      if ($membercode AND $memdisc > 0 AND empty($dispoutofstock) AND empty($dispwholesale))

      to:

      if ($membercode AND $memdisc > 0 AND empty($dispoutofstock) AND empty($dispwholesale) AND in_array($searchrow[ID], $allow_member_id))
  5. Open the order.php file in the /go/ folder.

    • Add the code:

      if(in_array($productid, $allow_member_id))

      just above the code (around line 152):

      $memdisc = (float)$memrow[Discount];
  6. Open the categories.php file in the /storeadmin/ folder.

    1. Around line 38, add:

      if($allowdiscounts!='Yes')
      $allowdiscounts='No';

      just under:

      $listimage=imgpathtourl($listimage, 'base');
    2. Around line 53, change:

      $updcatquery .= "Active='$active', HeaderImage='$headerimage', ListImage='$listimage', ";

      to:

      $updcatquery .= "Active='$active', AllowDiscounts='$allowdiscounts', HeaderImage='$headerimage', ListImage='$listimage', ";
    3. Around line 97, change:

      $inscatquery .= "Keywords, Active, HeaderImage, ListImage, CatColumns, CatRows, ";

      to:

      $inscatquery .= "Keywords, Active, AllowDiscounts, HeaderImage, ListImage, CatColumns, CatRows, ";
    4. A little further down, change:

      $inscatquery .= "'$keywords', '$active', '$headerimage', '$listimage', '$catcolumns', ";

      to:

      $inscatquery .= "'$keywords', '$active', '$allowdiscounts', '$headerimage', '$listimage', '$catcolumns', ";
    5. Around line 387, change:

      Active:

      to:

      Allow Discounts:


      Active:
  7. In the storeadmin area:

    1. Select 'Members' and add a member code and discount.

    2. After adding the member code:

      • Click 'Get Code' to create a link that your clients' customers will click to apply the coupon discount code

        - OR -

      • In the template, add the %COUPONS% include (coupon.php) to create a box on your site into which your clients' customers will type the member code you created above.

    3. In the 'Categories' area, edit any categories that will be discounted, and change Allow Discounts' to 'Yes'. Change those that should not be discounted to 'No'.

See also: Remove Member Discounts Mod for a simpler modification that does not require database changes.