Skip to content

Commit 1106636

Browse files
FPCSJamesspivurno
andauthored
gravity-forms/gw-coupons-exclude-products.php: Added multi-form support and a new parameter to disable exclusions when a 100% coupon is used.
Includes other miscellaneous fixes: - make $excluded_total an instance property - if a user had previously initiated multiple instances, unexpected behavior might result from the total being set out of order - add_init_script(): fix check for "gwcep" (always false, should be "gfecp") - add_init_script(): use JS object instead of array - output_script(): remove type attribute from script tag ("authors are encouraged to omit the attribute if the script refers to JavaScript code" - MDN) Co-authored-by: David Smith <[email protected]>
1 parent eacd626 commit 1106636

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

gravity-forms/gw-coupons-exclude-products.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
*
77
* Requires Gravity Forms Coupons v1.1
88
*
9-
* @version 1.2.2
10-
* @author David Smith <[email protected]>
11-
* @license GPL-2.0+
12-
* @link http://gravitywiz.com/...
9+
* @version 1.3
1310
*/
1411
class GW_Coupons_Exclude_Products {
1512

1613
protected static $is_script_output = false;
17-
public static $excluded_total = null;
1814

19-
public $_args = array();
15+
public $_args = array();
16+
public $excluded_total = null;
2017

2118
public function __construct( $args ) {
2219

2320
// set our default arguments, parse against the provided arguments, and store for use throughout the class
2421
$this->_args = wp_parse_args( $args, array(
25-
'form_id' => false,
26-
'exclude_fields' => array(),
22+
'form_id' => false,
23+
'exclude_fields' => array(),
24+
'exclude_fields_by_form' => array(),
25+
'skip_for_100_percent' => false,
2726
) );
2827

2928
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
@@ -36,7 +35,7 @@ function init() {
3635
$has_gravity_forms = property_exists( 'GFCommon', 'version' ) && version_compare( GFCommon::$version, '1.8', '>=' );
3736
$has_gf_coupons = class_exists( 'GFCoupons' );
3837

39-
// make sure we're running the required minimum version of Gravity Forms and GF Coupons
38+
// make sure we're running the required minimum version of Gravity Forms and that GF Coupons is installed
4039
if ( ! $has_gravity_forms || ! $has_gf_coupons ) {
4140
return;
4241
}
@@ -64,13 +63,13 @@ function load_form_script( $form ) {
6463
function output_script() {
6564
?>
6665

67-
<script type="text/javascript">
66+
<script>
6867

6968
( function( $ ) {
7069

7170
if( window.gform ) {
7271

73-
gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount, formId ) {
72+
gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount, formId ) {
7473

7574
price -= getExcludedAmount( formId );
7675

@@ -134,11 +133,12 @@ function add_init_script( $form ) {
134133
return;
135134
}
136135

136+
$base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) );
137137
$exclude_fields_json = json_encode( $this->_args['exclude_fields'] );
138138

139139
$script = "if( typeof gf_global != 'undefined' ) {
140-
if( typeof gf_global.gwcep == 'undefined' ) {
141-
gf_global.gfcep = [];
140+
if( typeof gf_global.gfcep == 'undefined' ) {
141+
gf_global.gfcep = {$base_json};
142142
}
143143
gf_global.gfcep[ {$this->_args['form_id']} ] = {$exclude_fields_json};
144144
}";
@@ -153,11 +153,11 @@ function stash_excluded_total( $product_data, $form ) {
153153
return $product_data;
154154
}
155155

156-
self::$excluded_total = 0;
156+
$this->excluded_total = 0;
157157

158158
foreach ( $product_data['products'] as $field_id => $data ) {
159159
if ( in_array( $field_id, $this->_args['exclude_fields'] ) ) {
160-
self::$excluded_total += GFCommon::to_number( $data['price'] );
160+
$this->excluded_total += GFCommon::to_number( $data['price'] );
161161
}
162162
}
163163

@@ -166,15 +166,15 @@ function stash_excluded_total( $product_data, $form ) {
166166

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

169-
if ( ! self::$excluded_total ) {
169+
if ( ! $this->excluded_total ) {
170170
return $discount;
171171
}
172172

173-
$price = $price - self::$excluded_total;
173+
$price = $price - $this->excluded_total;
174174
$currency = new RGCurrency( GFCommon::get_currency() );
175175
$amount = $currency->to_number( $coupon['amount'] );
176176

177-
if ( $coupon['type'] == 'percentage' ) {
177+
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
178178
$discount = $price * ( $amount / 100 );
179179
} elseif ( $coupon['type'] == 'flat' ) {
180180
$discount = $amount;
@@ -188,17 +188,44 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) {
188188

189189
function is_applicable_form( $form ) {
190190

191-
$coupon_fields = GFCommon::get_fields_by_type( $form, array( 'coupon' ) );
191+
$coupon_fields = GFCommon::get_fields_by_type( $form, array( 'coupon' ) );
192+
193+
if( sizeof( $this->_args['exclude_fields_by_form']) > 0 ) {
194+
$is_applicable_form_id = in_array( $form['id'], array_keys( $this->_args['exclude_fields_by_form'] ) );
195+
if( $is_applicable_form_id && $form['id'] != $this->_args['form_id'] ) {
196+
$this->_args['form_id'] = $form['id'];
197+
$this->_args['exclude_fields'] = $this->_args['exclude_fields_by_form'][$form['id']];
198+
}
199+
}
200+
192201
$is_applicable_form_id = $form['id'] == $this->_args['form_id'];
193202

194203
return $is_applicable_form_id && ! empty( $coupon_fields );
195204
}
196205

197206
}
198207

199-
# Configuration
208+
/**
209+
* Configuration
210+
* - for a single form, set form_id to your form ID, and exclude_fields to an array of the fields you wish to exclude
211+
* - 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
212+
* - set skip_for_100_percent to true to ignore these exclusions when a 100% off coupon is used
213+
*/
214+
215+
// Single form
216+
217+
new GW_Coupons_Exclude_Products( array(
218+
'form_id' => 123,
219+
'exclude_fields' => array( 4, 5 ),
220+
'skip_for_100_percent' => false
221+
) );
222+
223+
// Multiple forms
200224

201225
new GW_Coupons_Exclude_Products( array(
202-
'form_id' => 123,
203-
'exclude_fields' => array( 4, 5 ),
226+
'exclude_fields_by_form' => array(
227+
123 => array( 4, 5 ),
228+
456 => array( 7, 8 ),
229+
),
230+
'skip_for_100_percent' => false
204231
) );

0 commit comments

Comments
 (0)