The gform_coupons_discount_amount Gravity Forms PHP filter allows you to modify the coupon discount before it is applied to the form total.
Usage
add_filter('gform_coupons_discount_amount', 'your_custom_function', 10, 4); function your_custom_function($discount, $coupon, $price, $entry) { // your custom code here return $discount; }
Parameters
- $discount (float) – The current discount amount.
- $coupon (array) – Contains the coupon meta which is copied from the Coupons Feed Meta.
- $price (float) – The form total excluding discounts.
- $entry (Entry Object) – The current entry.
More information
See Gravity Forms Docs: gform_coupons_discount_amount
Examples
Adjust coupon discount for logged-in users
In this example, the coupon discount is increased by 5 if the user is logged in.
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; }
Apply a different discount for a specific coupon code
In this example, a custom discount is applied if the coupon code is “SPECIAL100”.
add_filter('gform_coupons_discount_amount', 'apply_special_discount', 10, 4); function apply_special_discount($discount, $coupon, $price, $entry) { if ($coupon['code'] === 'SPECIAL100') { $discount = $price * 0.1; // Apply a 10% discount } return $discount; }
Limit discount amount
In this example, the maximum discount amount is limited to 50.
add_filter('gform_coupons_discount_amount', 'limit_discount_amount', 10, 4); function limit_discount_amount($discount, $coupon, $price, $entry) { if ($discount > 50) { $discount = 50; } return $discount; }
Apply discount only to specific products
In this example, the discount is applied only to specific products in the form.
add_filter('gform_coupons_discount_amount', 'apply_discount_to_specific_products', 10, 4); function apply_discount_to_specific_products($discount, $coupon, $price, $entry) { $product_ids = array(1, 2, 3); // IDs of products you want to apply discount to $total_discount = 0; foreach ($entry as $field_id => $value) { if (in_array($field_id, $product_ids)) { $total_discount += $value; } } return $total_discount; }