Advertisement
Guest User

Use shortcode to total wedding meal choices

a guest
Jul 21st, 2014
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://pastebin.com/kHpaHQvi originally
  2. // http://www.gravityhelp.com/forums/topic/counter
  3. // usage: [guests form=1] where 1 is the form ID
  4. // from http://www.gravityhelp.com/forums/topic/counter#post-154024
  5. add_shortcode('guests', 'total_guests');
  6. function total_guests($atts) {
  7.         $form_id = $atts['form'];
  8.  
  9.         // function to pull all the entries for one form
  10.         // Chris Hajer April 15, 2014 to return more than 30 entries (the default)
  11.         $guests = RGFormsModel::get_leads($form_id, 0, 'ASC', '', 0, 999, NULL, NULL, FALSE, NULL, NULL, 'active', FALSE);
  12.  
  13.         // start the totals at zero
  14.         $adults = 0;
  15.  
  16.         // initialize a counter for the number of guests entries made
  17.         $i = 0;
  18.  
  19.         // loop through all the returned results
  20.         foreach ($guests as $amount) {
  21.  
  22.                 // total the adults
  23.                 // change 3 here to the Field ID which holds the number of adults
  24.                 $adults += $amount[3];
  25.  
  26.                 // increment the counter so we know how many total guests there are
  27.                 $i++; // we may not need to use this
  28.         }
  29.         // do some formatting and return the output from the shortcode
  30.         $output =       "$adults";
  31.  
  32.         // just the string above will be returned.
  33.         // You can style it here or where you are using the shortcode
  34.         return $output;
  35. }
  36. // needed for the above to process the guests list shortcode in sidebar widget
  37. add_filter('widget_text', 'do_shortcode');
  38.  
  39.  
  40. // shortcode to grab dinner selection totals from one form by value
  41. // usage: [dinner choice='whatever' formid=ID]
  42. // eg [dinner choice='Panko Cod' formid=4]
  43. if (!function_exists('ch_get_dinners')) {
  44.     function ch_get_dinners($atts) {
  45.         // parse the shortcode
  46.         extract(shortcode_atts(array(
  47.             'choice' => null,
  48.             'formid' => null
  49.         ), $atts));
  50.  
  51.         // define constants
  52.         $paging = array('offset' => 0, 'page_size' => 100 );
  53.         $sorting = array();
  54.         $total_count = 0;
  55.  
  56.         // define search criteria
  57.         $search_criteria = array();
  58.         $search_criteria["status"] = "active";
  59.         $search_criteria["field_filters"][] = array('value' => $choice);
  60.  
  61.         // get the entries which match the dinner choice
  62.         $entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging, $total_count);
  63.  
  64.         // initialize the counter
  65.         $counter = 0;
  66.  
  67.         // define which fields to check for dinner selections, based on Field ID
  68.         $all_fields = array(22, 24, 52, 61, 66);
  69.  
  70.         // loop through all the entries
  71.         foreach($entries as &$entry){
  72.             // check our 5 possible fields
  73.             foreach ($all_fields as &$field_id){
  74.                 if ($entry[$field_id] == $choice){
  75.                     $counter++;
  76.                 }
  77.             }
  78.         }
  79.         return $counter;
  80.     }
  81. }
  82. add_shortcode('dinner', 'ch_get_dinners');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement