GFPDFHelperFieldsField_Products

Controls the display and output of a Gravity Form field.

Defined (1)

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

/src/helper/fields/Field_Products.php  
  1. class Field_Products extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Checks if the form has any products 
  5. * @return boolean 
  6. * @since 4.0.2 
  7. */ 
  8. public function is_empty() { 
  9.  
  10. /** Set up the form / lead information */ 
  11. $form = $this->form; 
  12. $lead = $this->entry; 
  13.  
  14. /** Get all products for this field */ 
  15. $products = GFCommon::get_product_fields( $form, $lead, true ); 
  16.  
  17. if ( sizeof( $products['products'] ) > 0 ) { 
  18. return false; /** not empty */ 
  19.  
  20. return true; 
  21.  
  22. /** 
  23. * Return the HTML form data 
  24. * @return array 
  25. * @since 4.0 
  26. */ 
  27. public function form_data() { 
  28.  
  29. $data = $this->value(); 
  30.  
  31. return $data; 
  32.  
  33. /** 
  34. * Display the HTML version of this field 
  35. * @param string $value 
  36. * @param bool $label 
  37. * @return string 
  38. * @since 4.0 
  39. */ 
  40. public function html( $value = '', $label = true ) { 
  41. $products = $this->value(); 
  42. $form_id = $this->form['id']; 
  43.  
  44. /** start output buffer */ 
  45. ob_start(); 
  46.  
  47. ?> 
  48.  
  49. <div class="row-separator products-title-container"> 
  50. <h3 class="product-field-title gfpdf-field"> 
  51. <?php 
  52. $label = apply_filters( 'gform_order_label', esc_html__( 'Order', 'gravityforms' ), $form_id ); 
  53. $label = apply_filters( 'gform_order_label_' . $form_id, $label, $form_id ); 
  54.  
  55. echo $label; 
  56. ?> 
  57. </h3> 
  58. </div> 
  59.  
  60. <div class="row-separator products-container"> 
  61. <div class="gfpdf-field gfpdf-products"> 
  62. <div class="inner-container"> 
  63. <table class="entry-products" autosize="1"> 
  64. <tbody class="head"> 
  65. <tr> 
  66. <th class="entry-products-col1"> 
  67. <?php 
  68. $label = apply_filters( 'gform_product', esc_html__( 'Product', 'gravityforms' ), $form_id ); 
  69. $label = apply_filters( 'gform_product_' . $form_id, $label, $form_id ); 
  70.  
  71. echo $label; 
  72. ?> 
  73. </th> 
  74.  
  75. <th class="textcenter entry-products-col2"> 
  76. <?php 
  77. $label = apply_filters( 'gform_product_qty', esc_html__( 'Qty', 'gravityforms' ), $form_id ); 
  78. $label = apply_filters( 'gform_product_qty_' . $form_id, $label, $form_id ); 
  79.  
  80. echo $label; 
  81. ?> 
  82. </th> 
  83. <th class="entry-products-col3"> 
  84. <?php 
  85. $label = apply_filters( 'gform_product_unitprice', esc_html__( 'Unit Price', 'gravityforms' ), $form_id ); 
  86. $label = apply_filters( 'gform_product_unitprice_' . $form_id, $label, $form_id ); 
  87.  
  88. echo $label; 
  89. ?> 
  90. </th> 
  91. <th class="entry-products-col4"> 
  92. <?php 
  93. $label = apply_filters( 'gform_product_price', esc_html__( 'Price', 'gravityforms' ), $form_id ); 
  94. $label = apply_filters( 'gform_product_price_' . $form_id, $label, $form_id ); 
  95.  
  96. echo $label; 
  97. ?> 
  98. </th> 
  99. </tr> 
  100. </tbody> 
  101.  
  102. <tbody class="contents"> 
  103. <?php foreach ( $products['products'] as $product ) : ?> 
  104. <tr> 
  105. <td> 
  106. <div class="product_name"> 
  107. <?php echo $product['name']; ?> 
  108. </div> 
  109.  
  110. <?php 
  111. $price = $product['price_unformatted']; 
  112.  
  113. if ( sizeof( $product['options'] ) > 0 ) : ?> 
  114. <ul class="product_options"> 
  115. <?php foreach ( $product['options'] as $option ) : $price += $option['price']; ?> 
  116. <li><?php echo $option['option_label']; ?></li> 
  117. <?php endforeach; ?> 
  118. </ul> 
  119. <?php endif; ?> 
  120. </td> 
  121. <td class="textcenter"><?php echo $product['quantity']; ?></td> 
  122. <td><?php echo GFCommon::format_number( $price, 'currency' ); ?></td> 
  123. <td><?php echo $product['subtotal_formatted'] ?></td> 
  124. </tr> 
  125. <?php endforeach; ?> 
  126.  
  127. <?php if ( ! empty( $products['products_totals']['shipping_name'] ) ) : ?> 
  128. <tr> 
  129. <td rowspan="3" class="emptycell"></td> 
  130. <td colspan="2" 
  131. class="textright subtotal totals"><?php esc_html_e( 'Subtotal', 'gravity-forms-pdf-extended' ); ?></td> 
  132. <td class="subtotal_amount totals"><?php echo $products['products_totals']['subtotal_formatted']; ?></td> 
  133. </tr> 
  134. <tr> 
  135. <td colspan="2" 
  136. class="textright shipping totals"><?php echo sprintf( esc_html__( 'Shipping (%s)', 'gravity-forms-pdf-extended' ), $products['products_totals']['shipping_name'] ); ?></td> 
  137. <td class="shipping_amount totals"><?php echo $products['products_totals']['shipping_formatted']; ?></td> 
  138. </tr> 
  139. <?php endif; ?> 
  140.  
  141. <tr> 
  142. <?php if ( empty( $products['products_totals']['shipping_name'] ) ) : ?> 
  143. <td class="emptycell"></td> 
  144. <?php endif; ?> 
  145.  
  146. <td colspan="2" 
  147. class="textright grandtotal totals"><?php esc_html_e( 'Total', 'gravityforms' ) ?></td> 
  148. <td class="grandtotal_amount totals"><?php echo $products['products_totals']['total_formatted']; ?></td> 
  149. </tr> 
  150. </tbody> 
  151. </table> 
  152. </div> 
  153. </div> 
  154. </div> 
  155.  
  156. <?php 
  157.  
  158. return ob_get_clean(); 
  159.  
  160. /** 
  161. * Gravity Forms handles product fields in bulk as they are all linked together to get the order totals 
  162. * This class is used to handle this in bulk 
  163. * @return string|array 
  164. * @since 4.0 
  165. */ 
  166. public function value() { 
  167.  
  168. /** check if we have a value already stored in the cache */ 
  169. if ( $this->has_cache() ) { 
  170. return $this->cache(); 
  171.  
  172. /** Set up the form / lead information */ 
  173. $form = $this->form; 
  174. $lead = $this->entry; 
  175.  
  176. /** Get all products for this field */ 
  177. $use_value = (bool) apply_filters( 'gfpdf_show_field_value', false ); /** Set to `true` to show a field's value instead of the label */ 
  178. $products = GFCommon::get_product_fields( $form, $lead, ! $use_value ); 
  179.  
  180. /** Set up the appropriate varaibles needed for our product processing */ 
  181. $form_array = []; /** holds the actual product data */ 
  182. $order_total = 0; /** holds the total cost of the order */ 
  183.  
  184. /** check that there are actual product fields to process */ 
  185. if ( sizeof( $products['products'] ) > 0 ) { 
  186.  
  187. foreach ( $products['products'] as $id => $product ) { 
  188.  
  189. /** Get the raw pricing data */ 
  190. $product_raw_price = GFCommon::to_number( $product['price'] ); 
  191. $product_unit_price = $product_raw_price; 
  192.  
  193. /** Check if we should include options */ 
  194. $options = isset( $product['options'] ) ? $product['options'] : []; 
  195.  
  196. /** Process our options array */ 
  197. foreach ( $options as &$option ) { 
  198. /** Get the options raw price */ 
  199. $option_raw_price = GFCommon::to_number( $option['price'] ); 
  200.  
  201. /** Add the options price to the products price */ 
  202. $product_unit_price += $option_raw_price; 
  203.  
  204. /** add our formatted options price to the array */ 
  205. $option['price_formatted'] = GFCommon::to_money( $option_raw_price, $lead['currency'] ); 
  206.  
  207. /** Format our option strings correctly */ 
  208. $option['field_label'] = ( isset( $option['field_label'] ) ) ? esc_html( $option['field_label'] ) : ''; 
  209. $option['option_name'] = ( isset( $option['option_name'] ) ) ? esc_html( $option['option_name'] ) : ''; 
  210. $option['option_label'] = ( isset( $option['option_label'] ) ) ? esc_html( $option['option_label'] ) : ''; 
  211.  
  212. /** calculate subtotal */ 
  213. $product_subtotal = floatval( $product['quantity'] ) * $product_unit_price; 
  214.  
  215. /** increment the total */ 
  216. $order_total += $product_subtotal; 
  217.  
  218. /** Store product in $form_array array */ 
  219. $form_array['products'][ $id ] = [ 
  220. 'name' => esc_html( $product['name'] ),  
  221. 'price' => GFCommon::to_money( $product_raw_price, $lead['currency'] ),  
  222. 'price_unformatted' => $product_raw_price,  
  223. 'options' => $options,  
  224. 'quantity' => $product['quantity'],  
  225. 'subtotal' => $product_subtotal,  
  226. 'subtotal_formatted' => GFCommon::to_money( $product_subtotal, $lead['currency'] ),  
  227. ]; 
  228.  
  229. /** Increment total */ 
  230. $shipping_price = ( isset( $products['shipping']['price'] ) ) ? floatval( $products['shipping']['price'] ) : 0; 
  231. $order_total += $shipping_price; 
  232. $order_subtotal = $order_total - $shipping_price; 
  233.  
  234. /** add totals to form data */ 
  235. $form_array['products_totals'] = [ 
  236. 'subtotal' => $order_subtotal,  
  237. 'subtotal_formatted' => GFCommon::to_money( $order_subtotal, $lead['currency'] ),  
  238. 'shipping' => $shipping_price,  
  239. 'shipping_formatted' => GFCommon::to_money( $shipping_price, $lead['currency'] ),  
  240. 'shipping_name' => ( isset( $products['shipping']['name'] ) ) ? preg_replace( '/(.+?) \((.+?)\)/', '$2', $products['shipping']['name'] ) : '',  
  241. 'total' => $order_total,  
  242. 'total_formatted' => GFCommon::to_money( $order_total, $lead['currency'] ),  
  243. ]; 
  244.  
  245. $form_array['products_totals'] = array_map( 'esc_html', $form_array['products_totals'] ); 
  246.  
  247. /** Save the array into the cache */ 
  248. $this->cache( $form_array ); 
  249.  
  250. /** return the cache results */ 
  251.  
  252. return $this->cache();