Skip to content

Commit ce9b829

Browse files
committed
gw-coupons-exclude-products.php: Added functionality to exclude discounts on options of a product field.
1 parent 156046d commit ce9b829

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct( $args ) {
2121
$this->_args = wp_parse_args( $args, array(
2222
'form_id' => false,
2323
'exclude_fields' => array(),
24-
'exclude_fields_without_options' => array(),
24+
'exclude_options_from_fields' => array(),
2525
'exclude_fields_by_form' => array(),
2626
'skip_for_100_percent' => false,
2727
) );
@@ -104,7 +104,7 @@ function output_script() {
104104
function getExcludedAmount( formId ) {
105105

106106
var excludeFields = gf_global.gfcep[ formId ].exclude_fields,
107-
excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_fields_without_options,
107+
excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_options_from_fields,
108108
amount = 0;
109109

110110
if( ! excludeFields && ! excludeFieldsWithoutOptions ) {
@@ -144,15 +144,15 @@ function add_init_script( $form ) {
144144

145145
$base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) );
146146
$exclude_fields_json = json_encode( $this->_args['exclude_fields'] );
147-
$exclude_fields_without_options_json = json_encode( $this->_args['exclude_fields_without_options'] );
147+
$exclude_options_from_fields_json = json_encode( $this->_args['exclude_options_from_fields'] );
148148

149149
$script = "if( typeof gf_global != 'undefined' ) {
150150
if( typeof gf_global.gfcep == 'undefined' ) {
151151
gf_global.gfcep = {$base_json};
152152
}
153153
gf_global.gfcep[ {$this->_args['form_id']} ] = {
154154
exclude_fields: {$exclude_fields_json},
155-
exclude_fields_without_options: {$exclude_fields_without_options_json}
155+
exclude_options_from_fields: {$exclude_options_from_fields_json}
156156
};
157157
}";
158158

@@ -172,7 +172,7 @@ function stash_excluded_total( $product_data, $form ) {
172172
if ( in_array( $field_id, $this->_args['exclude_fields'] ) ) {
173173
$this->excluded_total += GFCommon::to_number( $data['price'] );
174174
}
175-
if ( in_array( $field_id, $this->_args['exclude_fields_without_options'] ) ) {
175+
if ( in_array( $field_id, $this->_args['exclude_options_from_fields'] ) ) {
176176
$this->excluded_total += GFCommon::to_number( $data['price'] );
177177
}
178178
}
@@ -186,12 +186,11 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) {
186186
return $discount;
187187
}
188188

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

194-
if ( $this->_args['exclude_fields_without_options'] ) {
193+
if ( $this->_args['exclude_options_from_fields'] ) {
195194
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
196195
$discount = $discount - ( $price * ( $amount / 100 ) );
197196
} else {
@@ -230,7 +229,7 @@ function is_applicable_form( $form ) {
230229

231230
/**
232231
* Configuration
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
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
234233
* - 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
235234
* - set skip_for_100_percent to true to ignore these exclusions when a 100% off coupon is used
236235
*/
@@ -247,7 +246,7 @@ function is_applicable_form( $form ) {
247246

248247
new GW_Coupons_Exclude_Products( array(
249248
'form_id' => 123,
250-
'exclude_fields_without_options' => array( 6 ),
249+
'exclude_options_from_fields' => array( 6 ),
251250
'skip_for_100_percent' => false,
252251
) );
253252

0 commit comments

Comments
 (0)