/includes/Fields/Product.php

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