evolve_WC_Form_Handler

The evolve evolve WC Form Handler class.

Defined (1)

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

/library/woo-config.php  
  1. class evolve_WC_Form_Handler extends WC_Form_Handler { 
  2.  
  3. public static function init() { 
  4. remove_action('template_redirect', array('WC_Form_Handler', 'save_address')); 
  5. add_action('template_redirect', array(__CLASS__, 'evolve_save_address')); 
  6.  
  7. public static function evolve_save_address() { 
  8. global $wp; 
  9.  
  10. $formvalue = ''; 
  11. if ( isset($_POST['formvalue']) ) { 
  12. $formvalue = $_POST['formvalue']; 
  13. $_SESSION['formvalue'] = $_POST['formvalue']; 
  14.  
  15. if( isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'], '/shipping' ) !== false ) { 
  16. $formvalue = 'shipping'; 
  17. $_SESSION['formvalue'] = 'shipping'; 
  18. } elseif ( isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'], '/billing' ) !== false ) { 
  19. $formvalue = 'billing'; 
  20. $_SESSION['formvalue'] = 'billing'; 
  21.  
  22. if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { 
  23. return; 
  24.  
  25. if (empty($_POST['action']) || 'edit_address' !== $_POST['action'] || empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-edit_address')) { 
  26. return; 
  27.  
  28. $user_id = get_current_user_id(); 
  29.  
  30. if ($user_id <= 0) { 
  31. return; 
  32.  
  33. $load_address = ( $formvalue == 'shipping' ) ? 'shipping' : 'billing'; 
  34.  
  35. $address = WC()->countries->get_address_fields(esc_attr($_POST[$load_address . '_country']), $load_address . '_'); 
  36.  
  37. foreach ($address as $key => $field) { 
  38. if (!isset($field['type'])) { 
  39. $field['type'] = 'text'; 
  40.  
  41. // Get Value. 
  42. switch ($field['type']) { 
  43. case 'checkbox' : 
  44. $_POST[$key] = isset($_POST[$key]) ? 1 : 0; 
  45. break; 
  46. default : 
  47. $_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : ''; 
  48. break; 
  49.  
  50. // Hook to allow modification of value. 
  51. $_POST[$key] = apply_filters('woocommerce_process_myaccount_field_' . $key, $_POST[$key]); 
  52.  
  53. // Validation: Required fields. 
  54. if (!empty($field['required']) && empty($_POST[$key])) { 
  55. wc_add_notice($field['label'] . ' ' . __('is a required field.', 'evolve'), 'error'); 
  56.  
  57. if (!empty($_POST[$key])) { 
  58.  
  59. // Validation rules 
  60. if (!empty($field['validate']) && is_array($field['validate'])) { 
  61. foreach ($field['validate'] as $rule) { 
  62. switch ($rule) { 
  63. case 'postcode' : 
  64. $_POST[$key] = strtoupper(str_replace(' ', '', $_POST[$key])); 
  65.  
  66. if (!WC_Validation::is_postcode($_POST[$key], $_POST[$load_address . '_country'])) { 
  67. wc_add_notice(__('Please enter a valid postcode/ZIP.', 'evolve'), 'error'); 
  68. } else { 
  69. $_POST[$key] = wc_format_postcode($_POST[$key], $_POST[$load_address . '_country']); 
  70. break; 
  71. case 'phone' : 
  72. $_POST[$key] = wc_format_phone_number($_POST[$key]); 
  73.  
  74. if (!WC_Validation::is_phone($_POST[$key])) { 
  75. wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'evolve'), 'error'); 
  76. break; 
  77. case 'email' : 
  78. $_POST[$key] = strtolower($_POST[$key]); 
  79.  
  80. if (!is_email($_POST[$key])) { 
  81. wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'evolve'), 'error'); 
  82. break; 
  83.  
  84. if (wc_notice_count('error') == 0) { 
  85.  
  86. foreach ($address as $key => $field) { 
  87. update_user_meta($user_id, $key, $_POST[$key]); 
  88.  
  89. wc_add_notice(__('Address changed successfully.', 'evolve')); 
  90.  
  91. do_action('woocommerce_customer_save_address', $user_id, $load_address); 
  92.  
  93. wp_safe_redirect(wc_get_page_permalink('myaccount')); 
  94. exit; 
  95.