Skip to content

Commit f2edc62

Browse files
authored
gw-coupons-exclude-products.php: Added functionality to exclude discounts on options of a product field.
1 parent c575683 commit f2edc62

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

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

Lines changed: 43 additions & 13 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_options_from_fields' => 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_options_from_fields,
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_options_from_fields_json = json_encode( $this->_args['exclude_options_from_fields'] );
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_options_from_fields: {$exclude_options_from_fields_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_options_from_fields'] ) ) {
176+
$this->excluded_total += GFCommon::to_number( $data['price'] );
177+
}
162178
}
163179

164180
return $product_data;
@@ -174,7 +190,13 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) {
174190
$currency = new RGCurrency( GFCommon::get_currency() );
175191
$amount = $currency->to_number( $coupon['amount'] );
176192

177-
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
193+
if ( $this->_args['exclude_options_from_fields'] ) {
194+
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
195+
$discount = $discount - ( $price * ( $amount / 100 ) );
196+
} else {
197+
$discount = $this->excluded_total;
198+
}
199+
} elseif ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
178200
$discount = $price * ( $amount / 100 );
179201
} elseif ( $coupon['type'] == 'flat' ) {
180202
$discount = $amount;
@@ -207,7 +229,7 @@ function is_applicable_form( $form ) {
207229

208230
/**
209231
* 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
232+
* - 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
211233
* - 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
212234
* - set skip_for_100_percent to true to ignore these exclusions when a 100% off coupon is used
213235
*/
@@ -220,6 +242,14 @@ function is_applicable_form( $form ) {
220242
'skip_for_100_percent' => false,
221243
) );
222244

245+
// Single form (exclude fields without options)
246+
247+
new GW_Coupons_Exclude_Products( array(
248+
'form_id' => 123,
249+
'exclude_options_from_fields' => array( 6 ),
250+
'skip_for_100_percent' => false,
251+
) );
252+
223253
// Multiple forms
224254

225255
new GW_Coupons_Exclude_Products( array(

0 commit comments

Comments
 (0)