Forums / Discounts/Coupons, Gift Certificates, Newsletters, Ads / Coupon Restriction for Specials - Solution

Coupon Restriction for Specials - Solution

Views: 18,902

Results 1 to 9 of 9
29 May 2012, 10:29 AM
#1
calljj avatar

calljj

Zen Follower

Join Date:
Mar 2007
Posts:
253
Plugin Contributions:
2

Coupon Restriction for Specials - Solution

Perhaps best for someone to package and put in dowmloads section and get all the glory :laugh:, just too busy at the moment to do it, but thought i would share a little mod to enable you to restrict a coupon from all items on special.
Its pretty basic in how it works, just two buttons added to the coupon restrictions page, an add and remove button.

The add button will search all products currently on special and will list them in the products retricted area, for that coupon for you.
the remove button removes all restricted products, that are on special for that coupon, from the restricted products.

File to change is /admin/coupon_restrict.php

The above is a core file so care to be taken, as always backup before performing any mod.

around line 36:

After:

 where restrict_id = '" . $_GET['info'] . "'");
    }
  }

add:

if ($_GET['action']=='specials')  {
$new_products_query = "select products_id from " . TABLE_SPECIALS . "  where status = 1 and products_id not in (select product_id from " . TABLE_COUPON_RESTRICT . " where coupon_id = '" . $_GET['cid'] . "')";
$new_products = $db->Execute($new_products_query);

while(!$new_products->EOF) {
// add all products from specials for each product not already defined in coupons_restrict
$db->Execute("insert into " . TABLE_COUPON_RESTRICT . " (coupon_id, product_id, coupon_restrict) values ('" . $_GET['cid'] . "', '" . $new_products->fields['products_id'] . "', 'Y')");
$new_products->MoveNext();
 }
}  

//remove specials
if ($_GET['action']=='remove_specials')  {
 // to delete existing products from coupon code that are selected as specials coupon_code that are already in the table
$new_products_query = "select products_id from " . TABLE_SPECIALS . "  where status = 1 and products_id in (select product_id from " . TABLE_COUPON_RESTRICT . " where coupon_id = '" . $_GET['cid'] . "')";
$new_products = $db->Execute($new_products_query);
 
// nothing to be done
          if ($new_products->RecordCount() == 0) {
            $messageStack->add(ERROR_DISCOUNT_COUPON_DEFINED_CATEGORY . ' ' . $_POST['cPath'], 'caution');
          }
          while(!$new_products->EOF) {
            // product needs to be deleted
          $db->Execute("delete from " . TABLE_COUPON_RESTRICT . " WHERE coupon_id = '" . $_GET['cid'] . "' and product_id = '" . $new_products->fields['products_id'] . "'");
            $new_products->MoveNext();
          }  
}

around line 387

After:

<!-- body_text_eof //-->
  </tr>
</table>

add:

<form name="restrict_specials" method="post" action="<?php echo zen_href_link('coupon_restrict.php', zen_get_all_get_params(array('info', 'action', 'x', 'y')) . 'action=specials&info=' . $cInfo->restrict_id, 'NONSSL'); ?>"><?php echo zen_draw_hidden_field('securityToken', $_SESSION['securityToken']); ?>
<input type="submit" name="add" value="Add Specials">
</form>
<form name="remove_specials" method="post" action="<?php echo zen_href_link('coupon_restrict.php', zen_get_all_get_params(array('info', 'action', 'x', 'y')) . 'action=remove_specials&info=' . $cInfo->restrict_id, 'NONSSL'); ?>"><?php echo zen_draw_hidden_field('securityToken', $_SESSION['securityToken']); ?>
<input type="submit" name="add" value="Remove Specials">
</form>

and that's it.

3 Aug 2012, 5:21 PM
#2
nfallat avatar

nfallat

New Zenner

Join Date:
Jan 2009
Posts:
84
Plugin Contributions:
0

Re: Coupon Restriction for Specials - Solution

Version 1.5

Thanks fo your contribution. But I tried this and it did not work to restrict the coupon on a special. There are actually 2 instances of where restrict_id = '" . $_GET

['info'] . "'");
}
}

I tried both places and did not work. Is there another trick to this?

Thanks!

4 Aug 2012, 9:39 AM
#3
calljj avatar

calljj

Zen Follower

Join Date:
Mar 2007
Posts:
253
Plugin Contributions:
2

Re: Coupon Restriction for Specials - Solution

You need to follow the instructions, no point putting code where it isn't needed.

You need to give me more info, what aspect isn't working?
Are the buttons shown?
Do they add products when you click to add them?
Do you have any products on special/discount at the moment?
After adding the products,usig the button, they still allowed to have a discount coupon working on them?

Its a really simple mod, so there really isn't much to go wrong.

21 Sep 2012, 3:03 PM
#4
iaing avatar

iaing

New Zenner

Join Date:
Feb 2012
Posts:
19
Plugin Contributions:
0

Re: Coupon Restriction for Specials - Solution

Worked perfectly for me thank you!!!
If anyone's trying it, remember that once you've pasted in the first block; for the second chunk, line 387 is no longer at line 387 as you've added several lines above it with the first paste (was line 414 on mine).

22 Jan 2014, 10:04 AM
#5
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: Coupon Restriction for Specials - Solution

Or if you just want to exclude all specials from the Coupon Order Total calculations you could just go to includes/modules/order_total/ot_coupon.php and do something like this:

Change line 470ish

if (!is_product_valid($products[$i]['id'], $couponCode)) {

to:

$specials_price = zen_get_products_special_price($products[$i]['id'], true);
if (!is_product_valid($products[$i]['id'], $couponCode) or $specials_price) {

Which stops you having to change the restrictions every time you change a special.

Nick
iszent.com

11 Aug 2014, 5:09 PM
#6
calljj avatar

calljj

Zen Follower

Join Date:
Mar 2007
Posts:
253
Plugin Contributions:
2

Re: Coupon Restriction for Specials - Solution

Someone recently pointed me back to this thread. the post above didn't work for them or me, but if I want to exclude all specials, easier to add a line to includes/functions/functions_general.php

around line 814 inside the "is_product_valid" function,

add under this line:

if ($coupons->RecordCount() == 0) return true;

$specials_price = zen_get_products_special_price((int)$product_id , true);
if ($specials_price) return false;

if your are using the bundles mod, you can also add:

if (isBundle((int)$product_id))  return false;

Hope this helps people out.

25 Dec 2014, 10:49 AM
#7
calljj avatar

calljj

Zen Follower

Join Date:
Mar 2007
Posts:
253
Plugin Contributions:
2

Re: Coupon Restriction for Specials - Solution

just an update, noticed that specials created under "salemaker" were not being restricted.

If you want to restrict those then add following line under the others above:

//if product in salemaker category
$product_check = $db->Execute("select master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . $product_id . "'");
$category_check = $product_check->fields['master_categories_id'];

  $saler = $db->Execute("select sale_specials_condition, sale_deduction_value, sale_deduction_type from " . TABLE_SALEMAKER_SALES . " where sale_categories_all like '%," . $category_check . ",%' and sale_status = '1' and (sale_date_start <= now() or sale_date_start = '0001-01-01') and (sale_date_end >= now() or sale_date_end = '0001-01-01')");
  if ($saler->RecordCount() >= 1) {
     return false;
  }
26 Dec 2014, 7:42 PM
#8
mclovin avatar

mclovin

New Zenner

Join Date:
Feb 2014
Location:
The Netherlands
Posts:
58
Plugin Contributions:
0

Re: Coupon Restriction for Specials - Solution

I'm afraid non of the solutions work for me... I don't get any error messages, but nothing changes either. :(

By the way, the is_product_valid function is on 769 for me. I'm running Zen Cart v 1.5.1.

28 Oct 2015, 6:09 PM
#9
dremhmrk2 avatar

dremhmrk2

New Zenner

Join Date:
Jun 2010
Posts:
32
Plugin Contributions:
0

Re: Coupon Restriction for Specials - Solution

niccol:

Or if you just want to exclude all specials from the Coupon Order Total calculations you could just go to includes/modules/order_total/ot_coupon.php and do something like this:

Change line 470ish

if (!is_product_valid($products[$i]['id'], $couponCode)) {

> 
> to:
> 
> ```
$specials_price = zen_get_products_special_price($products[$i]['id'], true);
if (!is_product_valid($products[$i]['id'], $couponCode) or $specials_price) {

Which stops you having to change the restrictions every time you change a special.

The above is what I just used. I added the following right after line 488 in includes/modules/order_total/ot_coupon.php:

// BOF Specials Coupon Restriction
	$specials_price = zen_get_products_special_price($products[$i]['id'], true);
if (!is_product_valid($products[$i]['id'], $couponCode) or $specials_price) {
// EOF Specials Coupon Restriction

The good news? Items not on special get their discounts while items on special do not, even when it's a mixed cart.

The bad news? There is no alert to tell the customer an item is being excluded, in fact it still gives the green A.O.K message.

The workaround? For now, I have appended a short notice to the TEXT_VALID_COUPON, which now reads:

//	BOF Edited TEXT_VALID_COUPON to add special restriction notice.
//	BOF Commented out for posterity
//	define('TEXT_VALID_COUPON', 'Congratulations you have redeemed the Discount Coupon.');
//	EOF Commented out for posterity

	define('TEXT_VALID_COUPON', 'Congratulations you have redeemed the Discount Coupon. Keep in mind that if your order contains items on special, the discount will not be applied for those items.');

//	EOF Edited TEXT_VALID_COUPON to add special restriction notice.

This is almost perfect but just not quite.

FYI: We are using v1.5.4