GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-total.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-total.php
4  * @package GravityView
5  * @subpackage includes\fields
6  * @since 1.20
7  */
8 
10 
11  var $name = 'total';
12 
13  var $is_searchable = true;
14 
15  var $is_numeric = true;
16 
17  var $search_operators = array( 'is', 'isnot', 'greater_than', 'less_than', 'contains' );
18 
19  var $group = 'product';
20 
21  var $icon = 'dashicons-cart';
22 
23  /** @see GF_Field_Total */
24  var $_gf_field_class_name = 'GF_Field_Total';
25 
26  public function __construct() {
27  $this->label = esc_html__( 'Total', 'gk-gravityview' );
28 
29  add_action( 'gravityview/edit_entry/after_update', array( $this, 'edit_entry_recalculate_totals' ), 10, 3 );
30 
31  add_filter( 'gravityview_blocklist_field_types', array( $this, 'add_to_blocklist' ), 10, 2 );
32 
33  parent::__construct();
34  }
35 
36  /**
37  * Prevent the Total fields from being displayed in the Edit Entry configuration screen -- for now
38  *
39  * Gravity Forms forms need to know all the pricing information available to calculate a Total.
40  *
41  * If you have an Edit Entry field with just two fields (Quantity and Total), the Total will not be able to calculate
42  * without the Product field, and possibly the Option, Shipping, and Coupon fields.
43  *
44  * The only options currently available are: show the whole form, or don't show the Total
45  *
46  * @since 1.20
47  *
48  * @todo Support Total fields in Edit Entry configuration
49  *
50  * @param array $blocklist Array of field types not able to be added to Edit Entry
51  * @param string|null $context Context
52  *
53  * @return array Blocklist, with "total" added. If not edit context, original field blocklist. Otherwise, blocklist including total.
54  */
55  public function add_to_blocklist( $blocklist = array(), $context = NULL ){
56 
57  if( empty( $context ) || $context !== 'edit' ) {
58  return $blocklist;
59  }
60 
61  $blocklist[] = 'total';
62 
63  return $blocklist;
64  }
65 
66  /**
67  * If entry has totals fields, recalculate them
68  *
69  * @since 1.20
70  *
71  * @param array $form Gravity Forms form array
72  * @param int $entry_id Gravity Forms Entry ID
73  * @param GravityView_Edit_Entry_Render $Edit_Entry_Render
74  *
75  * @return void
76  */
77  function edit_entry_recalculate_totals( $form = array(), $entry_id = 0, $Edit_Entry_Render = null ) {
78 
79  $original_form = GFAPI::get_form( $form['id'] );
80 
81  $total_fields = GFCommon::get_fields_by_type( $original_form, 'total' );
82 
83  //saving total field as the last field of the form.
84  if ( ! empty( $total_fields ) ) {
85 
86  $entry = GFAPI::get_entry( $entry_id );
87 
88  /** @type GF_Field_Total $total_field */
89  foreach ( $total_fields as $total_field ) {
90  $entry["{$total_field->id}"] = GFCommon::get_order_total( $original_form, $entry );
91  }
92 
93  $return_entry = GFAPI::update_entry( $entry );
94 
95  if( is_wp_error( $return_entry ) ) {
96  gravityview()->log->error( 'Updating the entry total fields failed', array( 'data' => $return_entry ) );
97  } else {
98  gravityview()->log->debug( 'Updating the entry total fields succeeded' );
99  }
100  }
101  }
102 }
103 
Modify field settings by extending this class.
add_to_blocklist( $blocklist=array(), $context=NULL)
Prevent the Total fields from being displayed in the Edit Entry configuration screen – for now...
if(gravityview() ->plugin->is_GF_25()) $form
edit_entry_recalculate_totals( $form=array(), $entry_id=0, $Edit_Entry_Render=null)
If entry has totals fields, recalculate them.
gravityview()
The main GravityView wrapper function.
$entry
Definition: notes.php:27