Exclude Products from Coupon Discount

Exclude specific products when calculating discounts with the Gravity Forms Coupons add-on.

Requires Gravity Forms Coupons v1.1

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gw-coupons-exclude-products.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
 * Gravity Wiz // Gravity Forms Coupons // Exclude Products from Coupon Discount
 *
 * Exclude specific products when calculating discounts with the Gravity Forms Coupons add-on.
 *
 * Requires Gravity Forms Coupons v1.1
 *
 * @version 1.4
 */
class GW_Coupons_Exclude_Products {

	protected static $is_script_output = false;

	public $_args          = array();
	public $excluded_total = null;

	public function __construct( $args ) {

		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args( $args, array(
			'form_id'                        => false,
			'exclude_fields'                 => array(),
			'exclude_options_from_fields' => array(),
			'exclude_fields_by_form'         => array(),
			'skip_for_100_percent'           => false,
		) );

		// do version check in the init to make sure if GF is going to be loaded, it is already loaded
		add_action( 'init', array( $this, 'init' ) );

	}

	function init() {

		$has_gravity_forms = property_exists( 'GFCommon', 'version' ) && version_compare( GFCommon::$version, '1.8', '>=' );
		$has_gf_coupons    = class_exists( 'GFCoupons' );

		// make sure we're running the required minimum version of Gravity Forms and that GF Coupons is installed
		if ( ! $has_gravity_forms || ! $has_gf_coupons ) {
			return;
		}

		// render
		add_filter( 'gform_pre_render', array( $this, 'load_form_script' ) );
		add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );

		// submission
		add_action( 'gform_product_info', array( $this, 'stash_excluded_total' ), 4 /* Coupons run on 5 */, 2 );
		add_filter( 'gform_coupons_discount_amount', array( $this, 'modify_coupon_discount_amount' ), 10, 3 );

	}

	function load_form_script( $form ) {

		if ( $this->is_applicable_form( $form ) && ! self::$is_script_output ) {
			add_action( 'wp_footer', array( $this, 'output_script' ) );
			add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
		}

		return $form;
	}

	function output_script() {
		?>

		<script>

			( function( $ ) {

				if( window.gform ) {

					gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount, formId ) {

						price -= getExcludedAmount( formId );

						if( couponType == 'percentage' ) {
							discount = price * Number( ( couponAmount / 100 ) );
						} else if( couponType == 'flat' ) {
							discount = Number( couponAmount );
							if( discount > price ) {
								discount = price;
							}
						}

						if( ! window.onTimeout && window.applyingCoupon ) {

							window.onTimeout = setTimeout( function() {

								window.onTimeout = false;
								window.applyingCoupon = false;

								$( document ).trigger( 'gform_post_conditional_logic' );

							}, 1 );

						}

						return discount;
					}, 15 );

				}

				function getExcludedAmount( formId ) {

					var excludeFields               = gf_global.gfcep[ formId ].exclude_fields,
						excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_options_from_fields,
						amount                      = 0;

					if( ! excludeFields && ! excludeFieldsWithoutOptions ) {
						return 0;
					}

					for( var i = 0; i < excludeFields.length; i++ ) {
						var productAmount = gformCalculateProductPrice( formId, excludeFields[ i ] );
						amount += productAmount;
					}

					for( var i = 0; i < excludeFieldsWithoutOptions.length; i++ ) {
						var productAmount = gformCalculateProductPrice( formId, excludeFieldsWithoutOptions[ i ] );
						var price = gformGetBasePrice( formId, excludeFieldsWithoutOptions[ i ] );
						var quantity = gformGetProductQuantity( formId, excludeFieldsWithoutOptions[ i ] );
						amount += ( productAmount - ( price * quantity ) );
					}

					return amount;
				}

			} )( jQuery );

		</script>

		<?php

		self::$is_script_output = true;

	}

	function add_init_script( $form ) {

		if ( ! $this->is_applicable_form( $form ) ) {
			return;
		}

		$base_json                           = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) );
		$exclude_fields_json                 = json_encode( $this->_args['exclude_fields'] );
		$exclude_options_from_fields_json = json_encode( $this->_args['exclude_options_from_fields'] );

		$script = "if( typeof gf_global != 'undefined' ) {
			if( typeof gf_global.gfcep == 'undefined' ) {
				gf_global.gfcep = {$base_json};
			}
			gf_global.gfcep[ {$this->_args['form_id']} ] = {
				exclude_fields: {$exclude_fields_json},
				exclude_options_from_fields: {$exclude_options_from_fields_json}
			};
		}";

		GFFormDisplay::add_init_script( $this->_args['form_id'], 'gfcep', GFFormDisplay::ON_PAGE_RENDER, $script );

	}

	function stash_excluded_total( $product_data, $form ) {

		if ( ! $this->is_applicable_form( $form ) ) {
			return $product_data;
		}

		$this->excluded_total = 0;

		foreach ( $product_data['products'] as $field_id => $data ) {
			if ( in_array( $field_id, $this->_args['exclude_fields'] ) ) {
				$this->excluded_total += GFCommon::to_number( $data['price'] );
			}
			if ( in_array( $field_id, $this->_args['exclude_options_from_fields'] ) ) {
				$this->excluded_total += GFCommon::to_number( $data['price'] );
			}
		}

		return $product_data;
	}

	function modify_coupon_discount_amount( $discount, $coupon, $price ) {

		if ( ! $this->excluded_total ) {
			return $discount;
		}

		$price    = $price - $this->excluded_total;
		$currency = new RGCurrency( GFCommon::get_currency() );
		$amount   = $currency->to_number( $coupon['amount'] );

		if ( $this->_args['exclude_options_from_fields'] ) {
			if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
				$discount = $discount - ( $price * ( $amount / 100 ) );
			} elseif ( $this->excluded_total < $discount) {
				$discount = $this->excluded_total;
			}
		} elseif ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
			$discount = $price * ( $amount / 100 );
		} elseif ( $coupon['type'] == 'flat' ) {
			$discount = $amount;
			if ( $discount > $price ) {
				$discount = $price;
			}
		}

		return $discount;
	}

	function is_applicable_form( $form ) {

		$coupon_fields = GFCommon::get_fields_by_type( $form, array( 'coupon' ) );

		if ( sizeof( $this->_args['exclude_fields_by_form'] ) > 0 ) {
			$is_applicable_form_id = in_array( $form['id'], array_keys( $this->_args['exclude_fields_by_form'] ) );
			if ( $is_applicable_form_id && $form['id'] != $this->_args['form_id'] ) {
				$this->_args['form_id']        = $form['id'];
				$this->_args['exclude_fields'] = $this->_args['exclude_fields_by_form'][ $form['id'] ];
			}
		}

		$is_applicable_form_id = $form['id'] == $this->_args['form_id'];

		return $is_applicable_form_id && ! empty( $coupon_fields );
	}

}

/**
 * Configuration
 * - for a single form, set form_id to your form ID, and exclude_fields to an array of the fields you wish to exclude or use exclude_options_from_fields for fields without options
 * - for multiple forms, set exclude_fields_by_form to an array with form IDs as its keys, and arrays of field IDs as its values
 * - set skip_for_100_percent to true to ignore these exclusions when a 100% off coupon is used
 */

// Single form

new GW_Coupons_Exclude_Products( array(
	'form_id'              => 123,
	'exclude_fields'       => array( 4, 5 ),
	'skip_for_100_percent' => false,
) );

// Single form (exclude fields without options)

new GW_Coupons_Exclude_Products( array(
	'form_id'                        => 123,
	'exclude_options_from_fields' => array( 6 ),
	'skip_for_100_percent'           => false,
) );

// Multiple forms

new GW_Coupons_Exclude_Products( array(
	'exclude_fields_by_form' => array(
		123 => array( 4, 5 ),
		456 => array( 7, 8 ),
	),
	'skip_for_100_percent'   => false,
) );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.