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
Parameter | Type | Description |
---|---|---|
$discount | float | The discount amount. |
$couponType | float | The couponAmountType from the Coupons Feed Meta. |
$couponAmount | float | The discount amount from the Coupons Feed Meta. |
$price | float | The form total excluding discounts. |
$totalDiscount | float | The total discount from all currently applied coupons. |
Examples
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 (isUserLoggedIn()) {
discount += 5;
}
return discount;
});
Placement
Your code snippet can be placed in an HTML field on your form or in a theme custom JavaScript file.
See also the JavaScript/jQuery section in this article: Where Do I Put This Code?
Source Code
This filter is located in GetDiscount() in js/coupons.js.
PHP Version
Parameter | Type | Description |
---|---|---|
$discount | float | The discount amount. |
$coupon | array | Contains the coupon meta which is copied from the Coupons Feed Meta. |
array(
'amount' => 100,
'name' => 'test100',
'type' => 'percentage',
'code' => 'TEST100',
'can_stack' => false,
'usage_count' => 2,
);
Parameter | Type | Description |
---|---|---|
$price | float | The form total excluding discounts. |
$entry | Entry Object | The current entry. |
Examples
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
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
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.