gform_coupons_discount_amount

Description

This filter can be used to modify the coupon discount before it is applied to the form total.

Usage

The gform_coupons_discount_amount filter has both a JavaScript version and a PHP version. Both versions should be used.

The JavaScript version only modifies the coupon discount on the front-end.

gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount ) {
// do stuff

return discount;
} );

The PHP version modifies the coupon discount during submission, ensuring that the pricing data used to create the entry and used with payment add-ons is updated.

add_filter( 'gform_coupons_discount_amount', function( $discount, $coupon, $price, $entry ) {
// do stuff

return $discount;
}, 10, 4 );

JavaScript Version

Parameters

Example

This example shows how you can adjust the coupon discount for logged-in users.

gform.addFilter( 'gform_coupons_discount_amount', function( discount, couponType, couponAmount, price, totalDiscount ) {
// you would need to write your own JS-version of is_user_logged_in()
if ( is_user_logged_in() ) {
discount += 5;
}
return discount;
} );

Placement

Your code snippet can be placed in a HTML field on your form or in a theme custom JavaScript file.

Source Code

This filter is located in GetDiscount() in js/coupons.js.

PHP Version

Parameters

array(
'amount'      => 100,
'name'        => 'test100',
'type'        => 'percentage',
'code'        => 'TEST100',
'can_stack'   => false,
'usage_count' => 2,
);
  • $price float

    The form total excluding discounts.

  • $entry Entry Object

    The current entry.

  • Example

    This example shows how you can adjust the coupon discount for logged-in users.

    add_filter( 'gform_coupons_discount_amount', 'add_logged_in_user_bonus_discount', 10, 4 );
    function add_logged_in_user_bonus_discount( $discount, $coupon, $price, $entry ) {
    if ( is_user_logged_in() ) {
    $discount += 5;
    }
    
    return $discount;
    }
    

    Placement

    Your code snippet should be placed in the functions.php file of your active theme.

    Source Code

    This filter is located in GFCoupons::get_discount() in class-gf-coupons.php.

    Version

    The $entry parameter was added to the PHP version of the hook in Coupons version 2.6.3.