Skip to content

Commit 92b8cec

Browse files
committed
gw-coupons-exclude-products.php: Added functionality to exclude discounts on options of a product field.
1 parent 45e73a5 commit 92b8cec

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

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

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Requires Gravity Forms Coupons v1.1
88
*
9-
* @version 1.3
9+
* @version 1.4
1010
*/
1111
class GW_Coupons_Exclude_Products {
1212

@@ -19,10 +19,11 @@ public function __construct( $args ) {
1919

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

2829
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
@@ -102,10 +103,11 @@ function output_script() {
102103

103104
function getExcludedAmount( formId ) {
104105

105-
var excludeFields = gf_global.gfcep[ formId ],
106-
amount = 0;
106+
var excludeFields = gf_global.gfcep[ formId ].exclude_fields,
107+
excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_fields_without_options,
108+
amount = 0;
107109

108-
if( ! excludeFields ) {
110+
if( ! excludeFields && ! excludeFieldsWithoutOptions ) {
109111
return 0;
110112
}
111113

@@ -114,6 +116,13 @@ function getExcludedAmount( formId ) {
114116
amount += productAmount;
115117
}
116118

119+
for( var i = 0; i < excludeFieldsWithoutOptions.length; i++ ) {
120+
var productAmount = gformCalculateProductPrice( formId, excludeFieldsWithoutOptions[ i ] );
121+
var price = gformGetBasePrice( formId, excludeFieldsWithoutOptions[ i ] );
122+
var quantity = gformGetProductQuantity( formId, excludeFieldsWithoutOptions[ i ] );
123+
amount += ( productAmount - ( price * quantity ) );
124+
}
125+
117126
return amount;
118127
}
119128

@@ -133,14 +142,18 @@ function add_init_script( $form ) {
133142
return;
134143
}
135144

136-
$base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) );
137-
$exclude_fields_json = json_encode( $this->_args['exclude_fields'] );
145+
$base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) );
146+
$exclude_fields_json = json_encode( $this->_args['exclude_fields'] );
147+
$exclude_fields_without_options_json = json_encode( $this->_args['exclude_fields_without_options'] );
138148

139149
$script = "if( typeof gf_global != 'undefined' ) {
140150
if( typeof gf_global.gfcep == 'undefined' ) {
141151
gf_global.gfcep = {$base_json};
142152
}
143-
gf_global.gfcep[ {$this->_args['form_id']} ] = {$exclude_fields_json};
153+
gf_global.gfcep[ {$this->_args['form_id']} ] = {
154+
exclude_fields: {$exclude_fields_json},
155+
exclude_fields_without_options: {$exclude_fields_without_options_json}
156+
};
144157
}";
145158

146159
GFFormDisplay::add_init_script( $this->_args['form_id'], 'gfcep', GFFormDisplay::ON_PAGE_RENDER, $script );
@@ -159,6 +172,9 @@ function stash_excluded_total( $product_data, $form ) {
159172
if ( in_array( $field_id, $this->_args['exclude_fields'] ) ) {
160173
$this->excluded_total += GFCommon::to_number( $data['price'] );
161174
}
175+
if ( in_array( $field_id, $this->_args['exclude_fields_without_options'] ) ) {
176+
$this->excluded_total += GFCommon::to_number( $data['price'] );
177+
}
162178
}
163179

164180
return $product_data;
@@ -170,11 +186,18 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) {
170186
return $discount;
171187
}
172188

173-
$price = $price - $this->excluded_total;
174-
$currency = new RGCurrency( GFCommon::get_currency() );
175-
$amount = $currency->to_number( $coupon['amount'] );
189+
$orig_price = $price;
190+
$price = $price - $this->excluded_total;
191+
$currency = new RGCurrency( GFCommon::get_currency() );
192+
$amount = $currency->to_number( $coupon['amount'] );
176193

177-
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
194+
if ( $this->_args['exclude_fields_without_options'] ) {
195+
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
196+
$discount = $discount - ( $price * ( $amount / 100 ) );
197+
} else {
198+
$discount = $this->excluded_total;
199+
}
200+
} elseif ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
178201
$discount = $price * ( $amount / 100 );
179202
} elseif ( $coupon['type'] == 'flat' ) {
180203
$discount = $amount;
@@ -207,13 +230,21 @@ function is_applicable_form( $form ) {
207230

208231
/**
209232
* 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
233+
* - 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_fields_without_options for fields without options
211234
* - 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
212235
* - set skip_for_100_percent to true to ignore these exclusions when a 100% off coupon is used
213236
*/
214237

215238
// Single form
216239

240+
new GW_Coupons_Exclude_Products( array(
241+
'form_id' => 123,
242+
'exclude_fields_without_options' => array( 6 ),
243+
'skip_for_100_percent' => false,
244+
) );
245+
246+
// Single form (exclude fields without options)
247+
217248
new GW_Coupons_Exclude_Products( array(
218249
'form_id' => 123,
219250
'exclude_fields' => array( 4, 5 ),

0 commit comments

Comments
 (0)