/src/helper/fields/Field_Products.php

  1. <?php 
  2.  
  3. namespace GFPDF\Helper\Fields; 
  4.  
  5. use GFPDF\Helper\Helper_Abstract_Fields; 
  6.  
  7. use GFCommon; 
  8.  
  9. /** 
  10. * Gravity Forms Field 
  11. * 
  12. * @package Gravity PDF 
  13. * @copyright Copyright (c) 2016, Blue Liquid Designs 
  14. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 
  15. * @since 4.0 
  16. */ 
  17.  
  18. /** Exit if accessed directly */ 
  19. if ( ! defined( 'ABSPATH' ) ) { 
  20. exit; 
  21.  
  22. /** 
  23. This file is part of Gravity PDF. 
  24.   
  25. Gravity PDF * Copyright (C) 2016, Blue Liquid Designs 
  26.   
  27. This program is free software; you can redistribute it and/or modify 
  28. it under the terms of the GNU General Public License as published by 
  29. the Free Software Foundation; either version 2 of the License, or 
  30. (at your option) any later version. 
  31.   
  32. This program is distributed in the hope that it will be useful,  
  33. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  34. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  35. GNU General Public License for more details. 
  36.   
  37. You should have received a copy of the GNU General Public License 
  38. along with this program; if not, write to the Free Software 
  39. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
  40. */ 
  41.  
  42. /** Include any dependancies */ 
  43. require_once( GFCommon::get_base_path() . '/currency.php' ); 
  44.  
  45. /** 
  46. * Controls the display and output of a Gravity Form field 
  47. * 
  48. * @since 4.0 
  49. */ 
  50. class Field_Products extends Helper_Abstract_Fields { 
  51.  
  52. /** 
  53. * Checks if the form has any products 
  54. * 
  55. * @return boolean 
  56. * 
  57. * @since 4.0.2 
  58. */ 
  59. public function is_empty() { 
  60.  
  61. /** Set up the form / lead information */ 
  62. $form = $this->form; 
  63. $lead = $this->entry; 
  64.  
  65. /** Get all products for this field */ 
  66. $products = GFCommon::get_product_fields( $form, $lead, true ); 
  67.  
  68. if ( sizeof( $products['products'] ) > 0 ) { 
  69. return false; /** not empty */ 
  70.  
  71. return true; 
  72.  
  73. /** 
  74. * Return the HTML form data 
  75. * 
  76. * @return array 
  77. * 
  78. * @since 4.0 
  79. */ 
  80. public function form_data() { 
  81.  
  82. $data = $this->value(); 
  83.  
  84. return $data; 
  85.  
  86. /** 
  87. * Display the HTML version of this field 
  88. * 
  89. * @param string $value 
  90. * @param bool $label 
  91. * 
  92. * @return string 
  93. * 
  94. * @since 4.0 
  95. */ 
  96. public function html( $value = '', $label = true ) { 
  97. $products = $this->value(); 
  98. $form_id = $this->form['id']; 
  99.  
  100. /** start output buffer */ 
  101. ob_start(); 
  102.  
  103. ?> 
  104.  
  105. <div class="row-separator products-title-container"> 
  106. <h3 class="product-field-title gfpdf-field"> 
  107. <?php 
  108. $label = apply_filters( 'gform_order_label', esc_html__( 'Order', 'gravityforms' ), $form_id ); 
  109. $label = apply_filters( 'gform_order_label_' . $form_id, $label, $form_id ); 
  110.  
  111. echo $label; 
  112. ?> 
  113. </h3> 
  114. </div> 
  115.  
  116. <div class="row-separator products-container"> 
  117. <div class="gfpdf-field gfpdf-products"> 
  118. <div class="inner-container"> 
  119. <table class="entry-products" autosize="1"> 
  120. <tbody class="head"> 
  121. <tr> 
  122. <th class="entry-products-col1"> 
  123. <?php 
  124. $label = apply_filters( 'gform_product', esc_html__( 'Product', 'gravityforms' ), $form_id ); 
  125. $label = apply_filters( 'gform_product_' . $form_id, $label, $form_id ); 
  126.  
  127. echo $label; 
  128. ?> 
  129. </th> 
  130.  
  131. <th class="textcenter entry-products-col2"> 
  132. <?php 
  133. $label = apply_filters( 'gform_product_qty', esc_html__( 'Qty', 'gravityforms' ), $form_id ); 
  134. $label = apply_filters( 'gform_product_qty_' . $form_id, $label, $form_id ); 
  135.  
  136. echo $label; 
  137. ?> 
  138. </th> 
  139. <th class="entry-products-col3"> 
  140. <?php 
  141. $label = apply_filters( 'gform_product_unitprice', esc_html__( 'Unit Price', 'gravityforms' ), $form_id ); 
  142. $label = apply_filters( 'gform_product_unitprice_' . $form_id, $label, $form_id ); 
  143.  
  144. echo $label; 
  145. ?> 
  146. </th> 
  147. <th class="entry-products-col4"> 
  148. <?php 
  149. $label = apply_filters( 'gform_product_price', esc_html__( 'Price', 'gravityforms' ), $form_id ); 
  150. $label = apply_filters( 'gform_product_price_' . $form_id, $label, $form_id ); 
  151.  
  152. echo $label; 
  153. ?> 
  154. </th> 
  155. </tr> 
  156. </tbody> 
  157.  
  158. <tbody class="contents"> 
  159. <?php foreach ( $products['products'] as $product ) : ?> 
  160. <tr> 
  161. <td> 
  162. <div class="product_name"> 
  163. <?php echo $product['name']; ?> 
  164. </div> 
  165.  
  166. <?php 
  167. $price = $product['price_unformatted']; 
  168.  
  169. if ( sizeof( $product['options'] ) > 0 ) : ?> 
  170. <ul class="product_options"> 
  171. <?php foreach ( $product['options'] as $option ) : $price += $option['price']; ?> 
  172. <li><?php echo $option['option_label']; ?></li> 
  173. <?php endforeach; ?> 
  174. </ul> 
  175. <?php endif; ?> 
  176. </td> 
  177. <td class="textcenter"><?php echo $product['quantity']; ?></td> 
  178. <td><?php echo GFCommon::format_number( $price, 'currency' ); ?></td> 
  179. <td><?php echo $product['subtotal_formatted'] ?></td> 
  180. </tr> 
  181. <?php endforeach; ?> 
  182.  
  183. <?php if ( ! empty( $products['products_totals']['shipping_name'] ) ) : ?> 
  184. <tr> 
  185. <td rowspan="3" class="emptycell"></td> 
  186. <td colspan="2" 
  187. class="textright subtotal totals"><?php esc_html_e( 'Subtotal', 'gravity-forms-pdf-extended' ); ?></td> 
  188. <td class="subtotal_amount totals"><?php echo $products['products_totals']['subtotal_formatted']; ?></td> 
  189. </tr> 
  190. <tr> 
  191. <td colspan="2" 
  192. class="textright shipping totals"><?php echo sprintf( esc_html__( 'Shipping (%s)', 'gravity-forms-pdf-extended' ), $products['products_totals']['shipping_name'] ); ?></td> 
  193. <td class="shipping_amount totals"><?php echo $products['products_totals']['shipping_formatted']; ?></td> 
  194. </tr> 
  195. <?php endif; ?> 
  196.  
  197. <tr> 
  198. <?php if ( empty( $products['products_totals']['shipping_name'] ) ) : ?> 
  199. <td class="emptycell"></td> 
  200. <?php endif; ?> 
  201.  
  202. <td colspan="2" 
  203. class="textright grandtotal totals"><?php esc_html_e( 'Total', 'gravityforms' ) ?></td> 
  204. <td class="grandtotal_amount totals"><?php echo $products['products_totals']['total_formatted']; ?></td> 
  205. </tr> 
  206. </tbody> 
  207. </table> 
  208. </div> 
  209. </div> 
  210. </div> 
  211.  
  212. <?php 
  213.  
  214. return ob_get_clean(); 
  215.  
  216. /** 
  217. * Gravity Forms handles product fields in bulk as they are all linked together to get the order totals 
  218. * This class is used to handle this in bulk 
  219. * 
  220. * @return string|array 
  221. * 
  222. * @since 4.0 
  223. */ 
  224. public function value() { 
  225.  
  226. /** check if we have a value already stored in the cache */ 
  227. if ( $this->has_cache() ) { 
  228. return $this->cache(); 
  229.  
  230. /** Set up the form / lead information */ 
  231. $form = $this->form; 
  232. $lead = $this->entry; 
  233.  
  234. /** Get all products for this field */ 
  235. $use_value = (bool) apply_filters( 'gfpdf_show_field_value', false ); /** Set to `true` to show a field's value instead of the label */ 
  236. $products = GFCommon::get_product_fields( $form, $lead, ! $use_value ); 
  237.  
  238. /** Set up the appropriate varaibles needed for our product processing */ 
  239. $form_array = []; /** holds the actual product data */ 
  240. $order_total = 0; /** holds the total cost of the order */ 
  241.  
  242. /** check that there are actual product fields to process */ 
  243. if ( sizeof( $products['products'] ) > 0 ) { 
  244.  
  245. foreach ( $products['products'] as $id => $product ) { 
  246.  
  247. /** Get the raw pricing data */ 
  248. $product_raw_price = GFCommon::to_number( $product['price'] ); 
  249. $product_unit_price = $product_raw_price; 
  250.  
  251. /** Check if we should include options */ 
  252. $options = isset( $product['options'] ) ? $product['options'] : []; 
  253.  
  254. /** Process our options array */ 
  255. foreach ( $options as &$option ) { 
  256. /** Get the options raw price */ 
  257. $option_raw_price = GFCommon::to_number( $option['price'] ); 
  258.  
  259. /** Add the options price to the products price */ 
  260. $product_unit_price += $option_raw_price; 
  261.  
  262. /** add our formatted options price to the array */ 
  263. $option['price_formatted'] = GFCommon::to_money( $option_raw_price, $lead['currency'] ); 
  264.  
  265. /** Format our option strings correctly */ 
  266. $option['field_label'] = ( isset( $option['field_label'] ) ) ? esc_html( $option['field_label'] ) : ''; 
  267. $option['option_name'] = ( isset( $option['option_name'] ) ) ? esc_html( $option['option_name'] ) : ''; 
  268. $option['option_label'] = ( isset( $option['option_label'] ) ) ? esc_html( $option['option_label'] ) : ''; 
  269.  
  270. /** calculate subtotal */ 
  271. $product_subtotal = floatval( $product['quantity'] ) * $product_unit_price; 
  272.  
  273. /** increment the total */ 
  274. $order_total += $product_subtotal; 
  275.  
  276. /** Store product in $form_array array */ 
  277. $form_array['products'][ $id ] = [ 
  278. 'name' => esc_html( $product['name'] ),  
  279. 'price' => GFCommon::to_money( $product_raw_price, $lead['currency'] ),  
  280. 'price_unformatted' => $product_raw_price,  
  281. 'options' => $options,  
  282. 'quantity' => $product['quantity'],  
  283. 'subtotal' => $product_subtotal,  
  284. 'subtotal_formatted' => GFCommon::to_money( $product_subtotal, $lead['currency'] ),  
  285. ]; 
  286.  
  287. /** Increment total */ 
  288. $shipping_price = ( isset( $products['shipping']['price'] ) ) ? floatval( $products['shipping']['price'] ) : 0; 
  289. $order_total += $shipping_price; 
  290. $order_subtotal = $order_total - $shipping_price; 
  291.  
  292. /** add totals to form data */ 
  293. $form_array['products_totals'] = [ 
  294. 'subtotal' => $order_subtotal,  
  295. 'subtotal_formatted' => GFCommon::to_money( $order_subtotal, $lead['currency'] ),  
  296. 'shipping' => $shipping_price,  
  297. 'shipping_formatted' => GFCommon::to_money( $shipping_price, $lead['currency'] ),  
  298. 'shipping_name' => ( isset( $products['shipping']['name'] ) ) ? preg_replace( '/(.+?) \((.+?)\)/', '$2', $products['shipping']['name'] ) : '',  
  299. 'total' => $order_total,  
  300. 'total_formatted' => GFCommon::to_money( $order_total, $lead['currency'] ),  
  301. ]; 
  302.  
  303. $form_array['products_totals'] = array_map( 'esc_html', $form_array['products_totals'] ); 
  304.  
  305. /** Save the array into the cache */ 
  306. $this->cache( $form_array ); 
  307.  
  308. /** return the cache results */ 
  309.  
  310. return $this->cache(); 
.