From 92b8ceca5ccb0c2d2ff7b76d5298f19c49b7ae03 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Mon, 3 Mar 2025 07:26:19 +0530 Subject: [PATCH 1/3] `gw-coupons-exclude-products.php`: Added functionality to exclude discounts on options of a product field. --- gravity-forms/gw-coupons-exclude-products.php | 63 ++++++++++++++----- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/gravity-forms/gw-coupons-exclude-products.php b/gravity-forms/gw-coupons-exclude-products.php index 02c9b551e..3addb4d78 100644 --- a/gravity-forms/gw-coupons-exclude-products.php +++ b/gravity-forms/gw-coupons-exclude-products.php @@ -6,7 +6,7 @@ * * Requires Gravity Forms Coupons v1.1 * - * @version 1.3 + * @version 1.4 */ class GW_Coupons_Exclude_Products { @@ -19,10 +19,11 @@ 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_fields_by_form' => array(), - 'skip_for_100_percent' => false, + 'form_id' => false, + 'exclude_fields' => array(), + 'exclude_fields_without_options' => 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 @@ -102,10 +103,11 @@ function output_script() { function getExcludedAmount( formId ) { - var excludeFields = gf_global.gfcep[ formId ], - amount = 0; + var excludeFields = gf_global.gfcep[ formId ].exclude_fields, + excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_fields_without_options, + amount = 0; - if( ! excludeFields ) { + if( ! excludeFields && ! excludeFieldsWithoutOptions ) { return 0; } @@ -114,6 +116,13 @@ function getExcludedAmount( formId ) { 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; } @@ -133,14 +142,18 @@ function add_init_script( $form ) { return; } - $base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) ); - $exclude_fields_json = json_encode( $this->_args['exclude_fields'] ); + $base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) ); + $exclude_fields_json = json_encode( $this->_args['exclude_fields'] ); + $exclude_fields_without_options_json = json_encode( $this->_args['exclude_fields_without_options'] ); $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_json}; + gf_global.gfcep[ {$this->_args['form_id']} ] = { + exclude_fields: {$exclude_fields_json}, + exclude_fields_without_options: {$exclude_fields_without_options_json} + }; }"; 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 ) { 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_fields_without_options'] ) ) { + $this->excluded_total += GFCommon::to_number( $data['price'] ); + } } return $product_data; @@ -170,11 +186,18 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) { return $discount; } - $price = $price - $this->excluded_total; - $currency = new RGCurrency( GFCommon::get_currency() ); - $amount = $currency->to_number( $coupon['amount'] ); + $orig_price = $price; + $price = $price - $this->excluded_total; + $currency = new RGCurrency( GFCommon::get_currency() ); + $amount = $currency->to_number( $coupon['amount'] ); - if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) { + if ( $this->_args['exclude_fields_without_options'] ) { + if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) { + $discount = $discount - ( $price * ( $amount / 100 ) ); + } else { + $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; @@ -207,13 +230,21 @@ function is_applicable_form( $form ) { /** * 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 + * - 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 * - 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_without_options' => array( 6 ), + 'skip_for_100_percent' => false, +) ); + +// Single form (exclude fields without options) + new GW_Coupons_Exclude_Products( array( 'form_id' => 123, 'exclude_fields' => array( 4, 5 ), From 156046d61ddf0e040de992d6266b754092aff605 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Mon, 3 Mar 2025 07:28:32 +0530 Subject: [PATCH 2/3] `gw-coupons-exclude-products.php`: Added functionality to exclude discounts on options of a product field. --- gravity-forms/gw-coupons-exclude-products.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gravity-forms/gw-coupons-exclude-products.php b/gravity-forms/gw-coupons-exclude-products.php index 3addb4d78..bc80ff48d 100644 --- a/gravity-forms/gw-coupons-exclude-products.php +++ b/gravity-forms/gw-coupons-exclude-products.php @@ -238,17 +238,17 @@ function is_applicable_form( $form ) { // Single form new GW_Coupons_Exclude_Products( array( - 'form_id' => 123, - 'exclude_fields_without_options' => array( 6 ), - 'skip_for_100_percent' => false, + '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_fields' => array( 4, 5 ), - 'skip_for_100_percent' => false, + 'form_id' => 123, + 'exclude_fields_without_options' => array( 6 ), + 'skip_for_100_percent' => false, ) ); // Multiple forms From ce9b8296932e3593448b617c2bf7fcd42560feae Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Sat, 8 Mar 2025 12:25:42 +0530 Subject: [PATCH 3/3] `gw-coupons-exclude-products.php`: Added functionality to exclude discounts on options of a product field. --- gravity-forms/gw-coupons-exclude-products.php | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/gravity-forms/gw-coupons-exclude-products.php b/gravity-forms/gw-coupons-exclude-products.php index bc80ff48d..3a7769ed1 100644 --- a/gravity-forms/gw-coupons-exclude-products.php +++ b/gravity-forms/gw-coupons-exclude-products.php @@ -21,7 +21,7 @@ public function __construct( $args ) { $this->_args = wp_parse_args( $args, array( 'form_id' => false, 'exclude_fields' => array(), - 'exclude_fields_without_options' => array(), + 'exclude_options_from_fields' => array(), 'exclude_fields_by_form' => array(), 'skip_for_100_percent' => false, ) ); @@ -104,7 +104,7 @@ function output_script() { function getExcludedAmount( formId ) { var excludeFields = gf_global.gfcep[ formId ].exclude_fields, - excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_fields_without_options, + excludeFieldsWithoutOptions = gf_global.gfcep[ formId ].exclude_options_from_fields, amount = 0; if( ! excludeFields && ! excludeFieldsWithoutOptions ) { @@ -144,7 +144,7 @@ function add_init_script( $form ) { $base_json = json_encode( array( 'skipFor100Percent' => $this->_args['skip_for_100_percent'] ) ); $exclude_fields_json = json_encode( $this->_args['exclude_fields'] ); - $exclude_fields_without_options_json = json_encode( $this->_args['exclude_fields_without_options'] ); + $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' ) { @@ -152,7 +152,7 @@ function add_init_script( $form ) { } gf_global.gfcep[ {$this->_args['form_id']} ] = { exclude_fields: {$exclude_fields_json}, - exclude_fields_without_options: {$exclude_fields_without_options_json} + exclude_options_from_fields: {$exclude_options_from_fields_json} }; }"; @@ -172,7 +172,7 @@ function stash_excluded_total( $product_data, $form ) { 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_fields_without_options'] ) ) { + if ( in_array( $field_id, $this->_args['exclude_options_from_fields'] ) ) { $this->excluded_total += GFCommon::to_number( $data['price'] ); } } @@ -186,12 +186,11 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) { return $discount; } - $orig_price = $price; - $price = $price - $this->excluded_total; - $currency = new RGCurrency( GFCommon::get_currency() ); - $amount = $currency->to_number( $coupon['amount'] ); + $price = $price - $this->excluded_total; + $currency = new RGCurrency( GFCommon::get_currency() ); + $amount = $currency->to_number( $coupon['amount'] ); - if ( $this->_args['exclude_fields_without_options'] ) { + if ( $this->_args['exclude_options_from_fields'] ) { if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) { $discount = $discount - ( $price * ( $amount / 100 ) ); } else { @@ -230,7 +229,7 @@ function is_applicable_form( $form ) { /** * 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_fields_without_options for fields without options + * - 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 */ @@ -247,7 +246,7 @@ function is_applicable_form( $form ) { new GW_Coupons_Exclude_Products( array( 'form_id' => 123, - 'exclude_fields_without_options' => array( 6 ), + 'exclude_options_from_fields' => array( 6 ), 'skip_for_100_percent' => false, ) );