Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hirejordansmith/348e6d32d3a1ed75bcb4e27f2bc8acb3 to your computer and use it in GitHub Desktop.
Save hirejordansmith/348e6d32d3a1ed75bcb4e27f2bc8acb3 to your computer and use it in GitHub Desktop.
Gravity Forms Field Value Sum
<?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