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_preview' ) ); 
  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 = floatval( $product[ 'product_price' ] ); 
  51.  
  52. if( isset( $related[ 'quantity' ][ 'value' ] ) && $related[ 'quantity' ][ 'value' ] ) { 
  53. $total = $total * $related[ 'quantity' ][ 'value' ]; 
  54. } elseif( $product[ 'product_use_quantity'] && $product[ 'value' ] ) { 
  55. $total = $total * $product[ 'value' ]; 
  56.  
  57. if( isset( $related[ 'modifier' ] ) ) { 
  58. //TODO: Handle multiple modifiers. 
  59.  
  60. $data[ 'product_totals' ][] = number_format( $total, 2 ); 
  61.  
  62. return $data; 
  63.  
  64. /** 
  65. * Validate 
  66. * @param $field 
  67. * @param $data 
  68. * @return array $errors 
  69. */ 
  70. public function validate( $field, $data ) 
  71. $errors = array(); 
  72.  
  73. if( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) { 
  74.  
  75. // Required check. 
  76. if( isset( $field['required'] ) && 1 == $field['required'] && ! trim( $field['value'] ) ) { 
  77. $errors[] = 'Field is required.'; 
  78.  
  79. return $errors; 
  80.  
  81. public function filter_required_setting( $field ) 
  82. if( 0 == $field->get_setting( 'product_use_quantity', 0 ) ) { 
  83. $field->update_setting('required', 0); 
  84. return $field; 
  85.  
  86. public function filter_required_setting_preview( $field ) 
  87. if( ! isset( $field[ 'settings' ][ 'product_use_quantity' ] ) || 1 != $field[ 'settings' ][ 'product_use_quantity' ] ) { 
  88. $field[ 'settings' ][ 'required' ] = 0; 
  89. return $field; 
  90.  
  91. public function merge_tag_value( $value, $field ) 
  92. $product_price = preg_replace ('/[^\d, \.]/', '', $field[ 'product_price' ] ); 
  93.  
  94. $product_quantity = ( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) ? $value : 1; 
  95.  
  96. return number_format( $product_price * $product_quantity, 2 );