NF_Fields_Product

Class NF_Fields_Product.

Defined (1)

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

/includes/Fields/Product.php  
  1. class NF_Fields_Product extends NF_Abstracts_Input 
  2. protected $_name = 'product'; 
  3.  
  4. protected $_section = 'pricing'; 
  5.  
  6. protected $_icon = 'tag'; 
  7.  
  8. protected $_aliases = array(); 
  9.  
  10. protected $_type = 'product'; 
  11.  
  12. protected $_templates = array( 'product', 'textbox', 'hidden', 'listselect' ); 
  13.  
  14. protected $_test_value = '0'; 
  15.  
  16. protected $processing_fields = array( 'quantity', 'modifier', 'shipping', 'tax', 'total' ); 
  17.  
  18. protected $_settings = array( 'product_use_quantity', 'product_price', 'product_type', 'product_type' ); 
  19.  
  20. protected $_settings_exclude = array( 'input_limit_set', 'disable_input', 'required' ); 
  21.  
  22. public function __construct() 
  23. parent::__construct(); 
  24.  
  25. $this->_nicename = __( 'Product', 'ninja-forms' ); 
  26.  
  27. $this->_settings[ 'product_price' ][ 'width' ] = 'full'; 
  28.  
  29. add_filter( 'ninja_forms_merge_tag_value_product', array( $this, 'merge_tag_value' ), 10, 2 ); 
  30.  
  31. add_filter( 'ninja_forms_localize_field_' . $this->_name, array( $this, 'filter_required_setting' ) ); 
  32. add_filter( 'ninja_forms_localize_field_' . $this->_name . '_preview', array( $this, 'filter_required_setting' ) ); 
  33.  
  34. public function process( $product, $data ) 
  35. $related = array(); 
  36.  
  37. foreach( $data[ 'fields' ] as $key => $field ) { 
  38.  
  39.  
  40. if( ! isset ( $field[ 'type' ] ) || ! in_array( $field[ 'type' ], $this->processing_fields ) ) continue; 
  41.  
  42. $type = $field[ 'type' ]; 
  43.  
  44. if( ! isset( $field[ 'product_assignment' ] ) ) continue; 
  45.  
  46. if( $product[ 'id' ] != $field[ 'product_assignment' ] ) continue; 
  47.  
  48. $related[ $type ] = &$data[ 'fields' ][ $key ]; // Assign by reference 
  49.  
  50. $total = str_replace( array( ', ', '$' ), '', $product[ 'product_price' ] ); 
  51. $total = floatval( $total ); 
  52.  
  53. if( isset( $related[ 'quantity' ][ 'value' ] ) && $related[ 'quantity' ][ 'value' ] ) { 
  54. $total = $total * $related[ 'quantity' ][ 'value' ]; 
  55. } elseif( $product[ 'product_use_quantity'] && $product[ 'value' ] ) { 
  56. $total = $total * $product[ 'value' ]; 
  57.  
  58. if( isset( $related[ 'modifier' ] ) ) { 
  59. //TODO: Handle multiple modifiers. 
  60.  
  61. $data[ 'product_totals' ][] = number_format( $total, 2 ); 
  62.  
  63. return $data; 
  64.  
  65. /** 
  66. * Validate 
  67. * @param $field 
  68. * @param $data 
  69. * @return array $errors 
  70. */ 
  71. public function validate( $field, $data ) 
  72. $errors = array(); 
  73.  
  74. if( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) { 
  75.  
  76. // Required check. 
  77. if( isset( $field['required'] ) && 1 == $field['required'] && ! trim( $field['value'] ) ) { 
  78. $errors[] = 'Field is required.'; 
  79.  
  80. return $errors; 
  81.  
  82. public function filter_required_setting( $field ) 
  83. if( ! isset( $field[ 'settings' ][ 'product_use_quantity' ] ) || 1 != $field[ 'settings' ][ 'product_use_quantity' ] ) { 
  84. $field[ 'settings' ][ 'required' ] = 0; 
  85. return $field; 
  86.  
  87. public function merge_tag_value( $value, $field ) 
  88. // TODO: Replaced this to fix English locales. 
  89. // Other locales are still broken and will need to be addressed in refactor. 
  90. // $product_price = preg_replace ('/[^\d, \.]/', '', $field[ 'product_price' ] ); 
  91. $product_price = str_replace( array( ', ', '$' ), '', $field[ 'product_price' ] ); 
  92.  
  93. $product_quantity = ( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) ? $value : 1; 
  94.  
  95. return number_format( $product_price * $product_quantity, 2 );