GFPDFHelperFieldsField_Product

Controls the individual display and output of: product, option, shipping, quantity and total fields.

Defined (1)

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

/src/helper/fields/Field_Product.php  
  1. class Field_Product extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Our products class which handles all Gravity Form products fields in bulk 
  5. * @var \GFPDF\Helper\Helper_Abstract_fields 
  6. */ 
  7. private $products; 
  8.  
  9. /** 
  10. * Store our products class for later user 
  11. * @param \GFPDF\Helper\Helper_Abstract_Fields $products 
  12. * @since 4.0 
  13. */ 
  14. public function set_products( Helper_Abstract_Fields $products ) { 
  15. $this->products = $products; 
  16.  
  17. /** 
  18. * Return the HTML form data 
  19. * @return array 
  20. * @since 4.0 
  21. */ 
  22. public function form_data() { 
  23.  
  24. $value = $this->value(); 
  25. $field = $this->field; 
  26. $label = GFFormsModel::get_label( $field ); 
  27. $field_id = (int) $field->id; 
  28. $data = []; 
  29. $name = $price = ''; 
  30.  
  31. switch ( $field->type ) { 
  32. case 'product': 
  33. $name = ( isset( $value['name'] ) && isset( $value['price'] ) ) ? $value['name'] . " ({$value['price']})" : ''; 
  34. $name = esc_html( $name ); 
  35.  
  36. $price = ( isset( $value['price_unformatted'] ) ) ? $value['price_unformatted'] : ''; 
  37. $price = esc_html( $price ); 
  38. break; 
  39.  
  40. case 'option': 
  41.  
  42. /** 
  43. * Gravity Forms doesn't currently store the option field ID with the standard product information. 
  44. * However, it does allow multiple fields to be an option for a single product. 
  45. * This becomes problematic when you have multiple option fields that contains the same name and are 
  46. * trying to determine which field it was selected from. 
  47. * To get around this limitation we'll process the entry option fields and make this data available. 
  48. */ 
  49.  
  50. /** Get the current option value for this field */ 
  51. $option_value = GFFormsModel::get_lead_field_value( $this->entry, $field ); 
  52.  
  53. /** Ensure this variable is an array */ 
  54. $option_value = ( ! is_array( $option_value ) ) ? [ $option_value ] : $option_value; 
  55.  
  56. /** Reset the array keys and remove any empty values */ 
  57. $option_value = array_values( $option_value ); 
  58. $option_value = array_filter( $option_value ); 
  59.  
  60. /** Get the field name ( */ 
  61. $name = array_map( function ( $value ) use ( $field ) { 
  62. $option_info = GFCommon::get_option_info( $value, $field, false ); 
  63.  
  64. return esc_html( $option_info['name'] ); 
  65. }, $option_value ); 
  66.  
  67. /** Get the field value (the price) */ 
  68. $price = array_map( function ( $value ) use ( $field ) { 
  69. $option_info = GFCommon::get_option_info( $value, $field, false ); 
  70.  
  71. return esc_html( $option_info['price'] ); 
  72. }, $option_value ); 
  73.  
  74. /** 
  75. * Valid option fields can only be radio, checkbox and select boxes 
  76. * To ensure backwards compatibility we'll remove the array if not a checkbox value 
  77. */ 
  78. if ( $field->inputType !== 'checkbox' ) { 
  79. $name = array_shift( $name ); 
  80. $price = array_shift( $price ); 
  81.  
  82. break; 
  83.  
  84. case 'shipping': 
  85. $name = ( isset( $value['shipping_name'] ) ) ? $value['shipping_name'] . " ({$value['shipping_formatted']})" : ''; 
  86. $name = esc_html( $name ); 
  87.  
  88. $price = ( isset( $value['shipping'] ) ) ? $value['shipping'] : ''; 
  89. $price = esc_html( $price ); 
  90. break; 
  91.  
  92. case 'quantity': 
  93. default: 
  94. $name = $value; 
  95. $price = $value; 
  96. break; 
  97.  
  98. /** Backwards Compatible * Standadised Format */ 
  99. $data['field'][ $field_id . '.' . $label ] = $name; 
  100. $data['field'][ $field_id ] = $name; 
  101. $data['field'][ $label ] = $name; 
  102.  
  103. /** Name Format */ 
  104. $data['field'][ $field_id . '.' . $label . '_name' ] = $name; 
  105. $data['field'][ $field_id . '_name' ] = $name; 
  106. $data['field'][ $label . '_name' ] = $name; 
  107.  
  108. /** New to v4 $form_data format to include the prices */ 
  109. $data['field'][ $field_id . '.' . $label . '_value' ] = $price; 
  110. $data['field'][ $field_id . '_value' ] = $price; 
  111. $data['field'][ $label . '_value' ] = $price; 
  112.  
  113. return $data; 
  114.  
  115. /** 
  116. * Display the HTML version of this field 
  117. * @param string $value 
  118. * @param bool $label 
  119. * @return string 
  120. * @since 4.0 
  121. */ 
  122. public function html( $value = '', $label = true ) { 
  123. $value = $this->value(); 
  124. $html = ''; 
  125.  
  126. switch ( $this->field->type ) { 
  127. case 'product': 
  128. if ( isset( $value['name'] ) ) { 
  129. $html .= esc_html( $value['name'] . ' - ' . $value['price'] ); 
  130. $html .= $this->get_option_html( $value['options'] ); 
  131. break; 
  132.  
  133. case 'option': 
  134. if ( isset( $value['options'] ) ) { 
  135. $html .= $this->get_option_html( $value['options'] ); 
  136. break; 
  137.  
  138. case 'quantity': 
  139. $html .= esc_html( $value ); 
  140. break; 
  141.  
  142. case 'shipping': 
  143. if ( isset( $value['shipping_formatted'] ) ) { 
  144. $html .= $value['shipping_formatted']; 
  145. break; 
  146.  
  147. case 'total': 
  148. if ( isset( $value['total_formatted'] ) ) { 
  149. $html .= $value['total_formatted']; 
  150. break; 
  151.  
  152. return parent::html( $html ); 
  153.  
  154. /** 
  155. * Get a HTML list of the product's selected options 
  156. * @param array $options A list of the selected products 
  157. * @param string $html Pass in an existing HTML, or default to blank 
  158. * @return string The finalised HTML 
  159. * @since 4.0 
  160. */ 
  161. public function get_option_html( $options, $html = '' ) { 
  162. if ( is_array( $options ) ) { 
  163. $html .= '<ul class="product_options">'; 
  164.  
  165. foreach ( $options as $option ) { 
  166. $html .= '<li>' . esc_html( $option['option_name'] . ' - ' . $option['price_formatted'] ) . '</li>'; 
  167.  
  168. $html .= '</ul>'; 
  169.  
  170. return $html; 
  171.  
  172. /** 
  173. * Get the standard GF value of this field 
  174. * @return string|array 
  175. * @since 4.0 
  176. * @internal We won't use a cache here because it's being handled in the Field_Products class, which is linked to this class through a static object 
  177. */ 
  178. public function value() { 
  179.  
  180. /** Get the full products array */ 
  181. $data = $this->products->value(); 
  182.  
  183. /** Filter out the product information we require */ 
  184. if ( $this->field->type == 'product' && isset( $data['products'][ $this->field->id ] ) ) { 
  185. return $data['products'][ $this->field->id ]; 
  186.  
  187. /** Filter out the options information we require */ 
  188. if ( $this->field->type == 'option' && isset( $data['products'][ $this->field->productField ]['options'] ) ) { 
  189. return [ 'options' => $data['products'][ $this->field->productField ]['options'] ]; 
  190.  
  191. /** Filter out the quantity field */ 
  192. if ( $this->field->type == 'quantity' ) { 
  193. return ( isset( $data['products'][ $this->field->productField ]['quantity'] ) ) ? $data['products'][ $this->field->productField ]['quantity'] : ''; 
  194.  
  195. /** Filter out the shipping field */ 
  196. if ( $this->field->type == 'shipping' && isset( $data['products_totals']['shipping'] ) ) { 
  197. return [ 
  198. 'shipping' => esc_html( $data['products_totals']['shipping'] ),  
  199. 'shipping_formatted' => esc_html( $data['products_totals']['shipping_formatted'] ),  
  200. 'shipping_name' => esc_html( $data['products_totals']['shipping_name'] ),  
  201. ]; 
  202.  
  203. /** Filter out the total field */ 
  204. if ( $this->field->type == 'total' && isset( $data['products_totals']['total'] ) ) { 
  205. return [ 
  206. 'total' => esc_html( $data['products_totals']['total'] ),  
  207. 'total_formatted' => esc_html( $data['products_totals']['total_formatted'] ),  
  208. ]; 
  209.  
  210. return [];