diff --git a/gravity-forms/gw-coupons-exclude-products.php b/gravity-forms/gw-coupons-exclude-products.php index 02c9b551e..3a7769ed1 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_options_from_fields' => 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_options_from_fields, + 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_options_from_fields_json = json_encode( $this->_args['exclude_options_from_fields'] ); $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_options_from_fields: {$exclude_options_from_fields_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_options_from_fields'] ) ) { + $this->excluded_total += GFCommon::to_number( $data['price'] ); + } } return $product_data; @@ -174,7 +190,13 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) { $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_options_from_fields'] ) { + 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,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 + * - 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 */ @@ -220,6 +242,14 @@ function is_applicable_form( $form ) { 'skip_for_100_percent' => false, ) ); +// Single form (exclude fields without options) + +new GW_Coupons_Exclude_Products( array( + 'form_id' => 123, + 'exclude_options_from_fields' => array( 6 ), + 'skip_for_100_percent' => false, +) ); + // Multiple forms new GW_Coupons_Exclude_Products( array(