Is there any way at all we can apply a 10% discount to a particular array of product fields, rather than the checkout total?
ChatGPT seems to think it can work using this filter, but it does not seem to affect anything.
add_filter( 'gform_coupons_discount_amount', function( $discount_amount, $coupon, $form, $entry ) {
// Check that it's the specific coupon
if ( strtolower($coupon['code']) === 'TOM10' ) { // <-- change to your coupon code (lowercase)
$eligible_total = 0;
$eligible_products = array( 48, 53, 54, 55 ); // <-- change to your field IDs
foreach ( $form['fields'] as $field ) {
if ( $field->type === 'product' ) {
$field_id = (string) $field->id;
$product_price = rgar( $entry, $field_id );
if ( in_array( $field->id, $eligible_products ) && is_numeric( $product_price ) ) {
$eligible_total += floatval( $product_price );
}
}
}
// New discount: 10% of eligible products
$discount_amount = $eligible_total * 0.10;
// Debugging: log the result
error_log( "Coupon applied: Eligible total = {$eligible_total}, Discount = {$discount_amount}" );
}
return $discount_amount;
}, 10, 4 );