Skip to content

Commit

Permalink
gravity-forms/gw-coupons-exclude-products.php: Added multi-form sup…
Browse files Browse the repository at this point in the history
…port and a new parameter to disable exclusions when a 100% coupon is used.

Includes other miscellaneous fixes:
- make $excluded_total an instance property - if a user had previously initiated multiple instances, unexpected behavior might result from the total being set out of order
- add_init_script(): fix check for "gwcep" (always false, should be "gfecp")
- add_init_script(): use JS object instead of array
- output_script(): remove type attribute from script tag ("authors are encouraged to omit the attribute if the script refers to JavaScript code" - MDN)

Co-authored-by: David Smith <[email protected]>
  • Loading branch information
FPCSJames and spivurno authored Jan 10, 2025
1 parent eacd626 commit 1106636
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions gravity-forms/gw-coupons-exclude-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
*
* Requires Gravity Forms Coupons v1.1
*
* @version 1.2.2
* @author David Smith <[email protected]>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @version 1.3
*/
class GW_Coupons_Exclude_Products {

protected static $is_script_output = false;
public static $excluded_total = null;

public $_args = array();
public $_args = array();
public $excluded_total = null;

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(),
'form_id' => false,
'exclude_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
Expand All @@ -36,7 +35,7 @@ function init() {
$has_gravity_forms = property_exists( 'GFCommon', 'version' ) && version_compare( GFCommon::$version, '1.8', '>=' );
$has_gf_coupons = class_exists( 'GFCoupons' );

// make sure we're running the required minimum version of Gravity Forms and GF Coupons
// make sure we're running the required minimum version of Gravity Forms and that GF Coupons is installed
if ( ! $has_gravity_forms || ! $has_gf_coupons ) {
return;
}
Expand Down Expand Up @@ -64,13 +63,13 @@ function load_form_script( $form ) {
function output_script() {
?>

<script type="text/javascript">
<script>

( function( $ ) {

if( window.gform ) {

gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount, formId ) {
gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount, formId ) {

Check warning on line 72 in gravity-forms/gw-coupons-exclude-products.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found precision alignment of 2 spaces.

Check failure on line 72 in gravity-forms/gw-coupons-exclude-products.php

View workflow job for this annotation

GitHub Actions / PHPCS

Tabs must be used to indent lines; spaces are not allowed

price -= getExcludedAmount( formId );

Expand Down Expand Up @@ -134,11 +133,12 @@ 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'] );

$script = "if( typeof gf_global != 'undefined' ) {
if( typeof gf_global.gwcep == 'undefined' ) {
gf_global.gfcep = [];
if( typeof gf_global.gfcep == 'undefined' ) {
gf_global.gfcep = {$base_json};
}
gf_global.gfcep[ {$this->_args['form_id']} ] = {$exclude_fields_json};
}";
Expand All @@ -153,11 +153,11 @@ function stash_excluded_total( $product_data, $form ) {
return $product_data;
}

self::$excluded_total = 0;
$this->excluded_total = 0;

foreach ( $product_data['products'] as $field_id => $data ) {
if ( in_array( $field_id, $this->_args['exclude_fields'] ) ) {
self::$excluded_total += GFCommon::to_number( $data['price'] );
$this->excluded_total += GFCommon::to_number( $data['price'] );
}
}

Expand All @@ -166,15 +166,15 @@ function stash_excluded_total( $product_data, $form ) {

function modify_coupon_discount_amount( $discount, $coupon, $price ) {

if ( ! self::$excluded_total ) {
if ( ! $this->excluded_total ) {
return $discount;
}

$price = $price - self::$excluded_total;
$price = $price - $this->excluded_total;
$currency = new RGCurrency( GFCommon::get_currency() );
$amount = $currency->to_number( $coupon['amount'] );

if ( $coupon['type'] == 'percentage' ) {
if ( $coupon['type'] == 'percentage' && ! ( $amount == 100 && $this->_args['skip_for_100_percent'] ) ) {
$discount = $price * ( $amount / 100 );
} elseif ( $coupon['type'] == 'flat' ) {
$discount = $amount;
Expand All @@ -188,17 +188,44 @@ function modify_coupon_discount_amount( $discount, $coupon, $price ) {

function is_applicable_form( $form ) {

$coupon_fields = GFCommon::get_fields_by_type( $form, array( 'coupon' ) );
$coupon_fields = GFCommon::get_fields_by_type( $form, array( 'coupon' ) );

if( sizeof( $this->_args['exclude_fields_by_form']) > 0 ) {
$is_applicable_form_id = in_array( $form['id'], array_keys( $this->_args['exclude_fields_by_form'] ) );
if( $is_applicable_form_id && $form['id'] != $this->_args['form_id'] ) {
$this->_args['form_id'] = $form['id'];

Check warning on line 196 in gravity-forms/gw-coupons-exclude-products.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
$this->_args['exclude_fields'] = $this->_args['exclude_fields_by_form'][$form['id']];
}
}

$is_applicable_form_id = $form['id'] == $this->_args['form_id'];

return $is_applicable_form_id && ! empty( $coupon_fields );
}

}

# Configuration
/**
* 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 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' => array( 4, 5 ),
'skip_for_100_percent' => false
) );

// Multiple forms

new GW_Coupons_Exclude_Products( array(
'form_id' => 123,
'exclude_fields' => array( 4, 5 ),
'exclude_fields_by_form' => array(
123 => array( 4, 5 ),
456 => array( 7, 8 ),
),
'skip_for_100_percent' => false
) );

0 comments on commit 1106636

Please sign in to comment.