Last active
July 7, 2019 19:21
-
-
Save hirejordansmith/348e6d32d3a1ed75bcb4e27f2bc8acb3 to your computer and use it in GitHub Desktop.
Gravity Forms Field Value Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_field_values_sum( $form_id, $input_id ) { | |
global $wpdb; | |
$query = array( | |
'select' => 'SELECT sum( ld.value )', | |
'from' => "FROM {$wpdb->prefix}rg_lead_detail ld", | |
'join' => "INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = ld.lead_id", | |
'where' => $wpdb->prepare( " | |
WHERE ld.form_id = %d | |
AND CAST( ld.field_number as unsigned ) = %d | |
AND l.status = 'active'", | |
$form_id, $input_id | |
) | |
); | |
$query = apply_filters( 'gwlimitbysum_query', $query, $form_id, $input_id ); | |
$query = apply_filters( 'gwinv_query', $query, $form_id, $input_id ); | |
$sql = implode( ' ', $query ); | |
$result = $wpdb->get_var( $sql ); | |
return intval( $result ); | |
} | |
// Assign the field value sum as a variable | |
// First number (5) is the form ID and second (2) is the Field ID | |
$total_sum = get_field_values_sum(5,2); | |
// You can then output the $total_sum wherever you'd like by creating a shortcode to capture and output that value | |
add_shortcode( 'totalsum', 'hjs_total_sum' ); | |
function hjs_total_sum() { | |
$sum_one = get_field_values_sum(5,3); | |
$sum_two = get_field_values_sum(5,4); | |
$total_sum = $sum_one + $sum_two; | |
return $total_sum; | |
} | |
// put [totalsum] anywhere you'd like to show the total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment