wooccmcountry_handler

The WooCommerce Checkout Manager wooccmcountry handler function.

Description

wooccmcountry_handler( $field, $key, $args, $value ); 

Parameters (4)

0. $field
The field.
1. $key
The key.
2. $args
The args.
3. $value
The value.

Usage

  1. if ( !function_exists( 'wooccmcountry_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-checkout-manager/includes/classes/field_filters.php'; 
  3.  
  4. // The field. 
  5. $field = null; 
  6.  
  7. // The key. 
  8. $key = null; 
  9.  
  10. // The args. 
  11. $args = null; 
  12.  
  13. // The value. 
  14. $value = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wooccmcountry_handler($field, $key, $args, $value); 
  18.  

Defined (1)

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

/includes/classes/field_filters.php  
  1. function wooccmcountry_handler( $field = '', $key, $args, $value ) { 
  2. global $current_user; 
  3. $user_roles = $current_user->roles; 
  4. $user_role = array_shift($user_roles); 
  5.  
  6. if( !empty($args['user_role']) && (!empty($args['role_options']) || !empty($args['role_options2'])) ) { 
  7. $rolekeys = explode('||', $args['role_options']); 
  8. $rolekeys2 = explode('||', $args['role_options2']); 
  9. if (!empty($args['role_options']) && !in_array($user_role, $rolekeys)) { 
  10. return; 
  11. if (!empty($args['role_options2']) && in_array($user_role, $rolekeys2)) { 
  12. return; 
  13. }  
  14.  
  15. if ( ( ! empty( $args['clear'] ) ) ) { 
  16. $after = '<div class="clear"></div>'; 
  17. } else { 
  18. $after = ''; 
  19.  
  20.  
  21. if ( $args['wooccm_required'] ) { 
  22. $args['class'][] = 'validate-required'; 
  23. $required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>'; 
  24. } else { 
  25. $required = ''; 
  26.  
  27. $args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : ''; 
  28.  
  29. if ( is_string( $args['label_class'] ) ) { 
  30. $args['label_class'] = array( $args['label_class'] ); 
  31.  
  32. if ( is_null( $value ) ) { 
  33. $value = $args['default']; 
  34.  
  35. // Custom attribute handling 
  36. $custom_attributes = array(); 
  37.  
  38. if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) { 
  39. foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) { 
  40. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; 
  41.  
  42. if ( ! empty( $args['validate'] ) ) { 
  43. foreach( $args['validate'] as $validate ) { 
  44. $args['class'][] = 'validate-' . $validate; 
  45.  
  46. $countries = $key == 'shipping_country' ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries(); 
  47.  
  48. if ( sizeof( $countries ) == 1 ) { 
  49.  
  50. $field = '<p class="form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $args['id'] ) . '_field">'; 
  51.  
  52. if ( $args['label'] ) { 
  53. $field .= '<label class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label'] . '</label>'; 
  54.  
  55. $field .= '<strong>' . current( array_values( $countries ) ) . '</strong>'; 
  56.  
  57. $field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys($countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" />'; 
  58.  
  59. if ( $args['description'] ) { 
  60. $field .= '<span class="description">' . esc_attr( $args['description'] ) . '</span>'; 
  61.  
  62. $field .= '</p>' . $after; 
  63.  
  64. } else { 
  65.  
  66. $field = '<p class="form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $args['id'] ) . '_field">' 
  67. . '<label for="' . esc_attr( $args['id'] ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label'] . $required . '</label>' 
  68. . '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country_to_state country_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" ' . implode( ' ', $custom_attributes ) . '>' 
  69. . '<option value="">'.__( 'Select a country…', 'woocommerce' ) .'</option>'; 
  70.  
  71. foreach ( $countries as $ckey => $cvalue ) { 
  72. $field .= '<option value="' . esc_attr( $ckey ) . '" '.selected( $value, $ckey, false ) .'>'.__( $cvalue, 'woocommerce' ) .'</option>'; 
  73.  
  74. $field .= '</select>'; 
  75.  
  76. $field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . __( 'Update country', 'woocommerce' ) . '" /></noscript>'; 
  77.  
  78. if ( $args['description'] ) { 
  79. $field .= '<span class="description">' . esc_attr( $args['description'] ) . '</span>'; 
  80.  
  81. $field .= '</p>' . $after; 
  82.  
  83. return $field;