GF_Field_SingleProduct

The Gravity Forms GF Field SingleProduct class.

Defined (1)

The class is defined in the following location(s).

/includes/fields/class-gf-field-singleproduct.php  
  1. class GF_Field_SingleProduct extends GF_Field { 
  2.  
  3. public $type = 'singleproduct'; 
  4.  
  5. function get_form_editor_field_settings() { 
  6. return array( 
  7. 'base_price_setting',  
  8. 'disable_quantity_setting',  
  9. 'conditional_logic_field_setting',  
  10. 'error_message_setting',  
  11. 'label_setting',  
  12. 'rules_setting',  
  13. 'duplicate_setting',  
  14. ); 
  15.  
  16. public function get_form_editor_button() { 
  17. return array(); 
  18.  
  19. public function validate( $value, $form ) { 
  20. $quantity_id = $this->id . '.3'; 
  21. $quantity = rgget( $quantity_id, $value ); 
  22.  
  23. if ( $this->isRequired && rgblank( $quantity ) && ! $this->disableQuantity ) { 
  24. $this->failed_validation = true; 
  25. $this->validation_message = empty($this->errorMessage) ? esc_html__( 'This field is required.', 'gravityforms' ) : $this->errorMessage; 
  26. } elseif ( ! empty( $quantity ) && ( ! is_numeric( $quantity ) || intval( $quantity ) != floatval( $quantity ) || intval( $quantity ) < 0 ) ) { 
  27. $this->failed_validation = true; 
  28. $this->validation_message = esc_html__( 'Please enter a valid quantity', 'gravityforms' ); 
  29.  
  30.  
  31. public function get_field_input( $form, $value = '', $entry = null ) { 
  32. $form_id = $form['id']; 
  33. $is_entry_detail = $this->is_entry_detail(); 
  34. $is_form_editor = $this->is_form_editor(); 
  35.  
  36. $id = (int) $this->id; 
  37. $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; 
  38.  
  39. $product_name = ! is_array( $value ) || empty( $value[ $this->id . '.1' ] ) ? esc_attr( $this->label ) : esc_attr( $value[ $this->id . '.1' ] ); 
  40. $price = ! is_array( $value ) || empty( $value[ $this->id . '.2' ] ) ? $this->basePrice : esc_attr( $value[ $this->id . '.2' ] ); 
  41. $quantity = is_array( $value ) ? esc_attr( $value[ $this->id . '.3' ] ) : ''; 
  42.  
  43. if ( empty( $price ) ) { 
  44. $price = 0; 
  45.  
  46. $has_quantity = sizeof( GFCommon::get_product_fields_by_type( $form, array( 'quantity' ), $this->id ) ) > 0; 
  47. if ( $has_quantity ) { 
  48. $this->disableQuantity = true; 
  49.  
  50. $currency = $is_entry_detail && ! empty( $entry ) ? $entry['currency'] : ''; 
  51.  
  52. $quantity_field = ''; 
  53.  
  54. $qty_input_type = GFFormsModel::is_html5_enabled() ? 'number' : 'text'; 
  55.  
  56. $qty_min_attr = GFFormsModel::is_html5_enabled() ? "min='0'" : ''; 
  57.  
  58. $product_quantity_sub_label = gf_apply_filters( array( 'gform_product_quantity', $form_id, $this->id ), esc_html__( 'Quantity:', 'gravityforms' ), $form_id ); 
  59.  
  60. if ( $is_entry_detail || $is_form_editor ) { 
  61. $disabled_text = $is_form_editor ? 'disabled="disabled"' : ''; 
  62. $style = $this->disableQuantity ? "style='display:none;'" : ''; 
  63. $quantity_field = " <span class='ginput_quantity_label' {$style}>{$product_quantity_sub_label}</span> <input type='{$qty_input_type}' name='input_{$id}.3' value='{$quantity}' id='ginput_quantity_{$form_id}_{$this->id}' class='ginput_quantity' size='10' {$disabled_text}/>"; 
  64. } else if ( ! $this->disableQuantity ) { 
  65. $tabindex = $this->get_tabindex(); 
  66. $quantity_field .= " <span class='ginput_quantity_label'>" . $product_quantity_sub_label . "</span> <input type='{$qty_input_type}' name='input_{$id}.3' value='{$quantity}' id='ginput_quantity_{$form_id}_{$this->id}' class='ginput_quantity' size='10' {$qty_min_attr} {$tabindex}/>"; 
  67. } else { 
  68. if ( ! is_numeric( $quantity ) ) { 
  69. $quantity = 1; 
  70.  
  71. if ( ! $has_quantity ) { 
  72. $quantity_field .= "<input type='hidden' name='input_{$id}.3' value='{$quantity}' class='ginput_quantity_{$form_id}_{$this->id} gform_hidden' />"; 
  73.  
  74. return "<div class='ginput_container ginput_container_singleproduct'> 
  75. <input type='hidden' name='input_{$id}.1' value='{$product_name}' class='gform_hidden' /> 
  76. <span class='ginput_product_price_label'>" . gf_apply_filters( array( 'gform_product_price', $form_id, $this->id ), esc_html__( 'Price', 'gravityforms' ), $form_id ) . ":</span> <span class='ginput_product_price' id='{$field_id}'>" . esc_html( GFCommon::to_money( $price, $currency ) ) . "</span> 
  77. <input type='hidden' name='input_{$id}.2' id='ginput_base_price_{$form_id}_{$this->id}' class='gform_hidden' value='" . esc_attr( $price ) . "'/> 
  78. {$quantity_field} 
  79. </div>"; 
  80.  
  81. public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) { 
  82. if ( is_array( $value ) && ! empty( $value ) ) { 
  83. $product_name = trim( $value[ $this->id . '.1' ] ); 
  84. $price = trim( $value[ $this->id . '.2' ] ); 
  85. $quantity = trim( $value[ $this->id . '.3' ] ); 
  86.  
  87. $product = $product_name . ', ' . esc_html__( 'Qty: ', 'gravityforms' ) . $quantity . ', ' . esc_html__( 'Price: ', 'gravityforms' ) . $price; 
  88.  
  89. return $product; 
  90. } else { 
  91. return ''; 
  92.