woo-checkout-field-editor-pro
Last commit date
assets
8 years ago
classes
8 years ago
checkout-form-designer.php
8 years ago
readme.txt
8 years ago
checkout-form-designer.php
300 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Woo Checkout Field Editor Pro |
| 4 | * Description: Customize WooCommerce checkout fields(Add, Edit, Delete and re-arrange fields). |
| 5 | * Author: ThemeHiGH |
| 6 | * Version: 1.0.9 |
| 7 | * Author URI: http://www.themehigh.com |
| 8 | * Plugin URI: http://www.themehigh.com |
| 9 | * Text Domain: thwcfd |
| 10 | * Domain Path: /languages |
| 11 | */ |
| 12 | |
| 13 | if(!defined( 'ABSPATH' )) exit; |
| 14 | |
| 15 | if(in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
| 16 | load_plugin_textdomain( 'thwcfd', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 17 | |
| 18 | /** |
| 19 | * woocommerce_init_checkout_field_editor function. |
| 20 | */ |
| 21 | function thwcfd_init_checkout_field_editor_lite() { |
| 22 | global $supress_field_modification; |
| 23 | $supress_field_modification = false; |
| 24 | |
| 25 | if(!class_exists('WC_Checkout_Field_Editor')){ |
| 26 | require_once('classes/class-wc-checkout-field-editor.php'); |
| 27 | } |
| 28 | |
| 29 | if (!class_exists('WC_Checkout_Field_Editor_Export_Handler')){ |
| 30 | require_once('classes/class-wc-checkout-field-editor-export-handler.php'); |
| 31 | } |
| 32 | new WC_Checkout_Field_Editor_Export_Handler(); |
| 33 | |
| 34 | $GLOBALS['WC_Checkout_Field_Editor'] = new WC_Checkout_Field_Editor(); |
| 35 | } |
| 36 | add_action('init', 'thwcfd_init_checkout_field_editor_lite'); |
| 37 | |
| 38 | /** |
| 39 | * Hide Additional Fields title if no fields available. |
| 40 | * |
| 41 | * @param mixed $old |
| 42 | */ |
| 43 | function thwcfd_enable_order_notes_field() { |
| 44 | global $supress_field_modification; |
| 45 | |
| 46 | if($supress_field_modification){ |
| 47 | return $fields; |
| 48 | } |
| 49 | |
| 50 | $additional_fields = get_option('wc_fields_additional'); |
| 51 | if(is_array($additional_fields)){ |
| 52 | $enabled = 0; |
| 53 | foreach($additional_fields as $field){ |
| 54 | if($field['enabled']){ |
| 55 | $enabled++; |
| 56 | } |
| 57 | } |
| 58 | return $enabled > 0 ? true : false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | add_filter('woocommerce_enable_order_notes_field', 'thwcfd_enable_order_notes_field', 1000); |
| 63 | |
| 64 | /*function thwcfd_woo_default_address_fields( $address_fields ) { |
| 65 | $address_fields['state']['label'] = 'County'; |
| 66 | return $address_fields; |
| 67 | } |
| 68 | add_filter( 'woocommerce_default_address_fields' , 'thwcfd_woo_default_address_fields' );*/ |
| 69 | |
| 70 | /** |
| 71 | * wc_checkout_fields_modify_billing_fields function. |
| 72 | * |
| 73 | * @param mixed $old |
| 74 | */ |
| 75 | function thwcfd_billing_fields_lite($old){ |
| 76 | global $supress_field_modification; |
| 77 | |
| 78 | if($supress_field_modification){ |
| 79 | return $old; |
| 80 | } |
| 81 | |
| 82 | return thwcfd_prepare_checkout_fields_lite(get_option('wc_fields_billing'), $old); |
| 83 | } |
| 84 | add_filter('woocommerce_billing_fields', 'thwcfd_billing_fields_lite', 1000); |
| 85 | |
| 86 | /** |
| 87 | * wc_checkout_fields_modify_shipping_fields function. |
| 88 | * |
| 89 | * @param mixed $old |
| 90 | */ |
| 91 | function thwcfd_shipping_fields_lite($old){ |
| 92 | global $supress_field_modification; |
| 93 | |
| 94 | if ($supress_field_modification){ |
| 95 | return $old; |
| 96 | } |
| 97 | |
| 98 | return thwcfd_prepare_checkout_fields_lite(get_option('wc_fields_shipping'), $old); |
| 99 | } |
| 100 | add_filter('woocommerce_shipping_fields', 'thwcfd_shipping_fields_lite', 1000); |
| 101 | |
| 102 | /** |
| 103 | * wc_checkout_fields_modify_shipping_fields function. |
| 104 | * |
| 105 | * @param mixed $old |
| 106 | */ |
| 107 | function thwcfd_checkout_fields_lite( $fields ) { |
| 108 | global $supress_field_modification; |
| 109 | |
| 110 | if($supress_field_modification){ |
| 111 | return $fields; |
| 112 | } |
| 113 | |
| 114 | if($additional_fields = get_option('wc_fields_additional')){ |
| 115 | $fields['order'] = $additional_fields + $fields['order']; |
| 116 | |
| 117 | // check if order_comments is enabled/disabled |
| 118 | if(isset($additional_fields) && !$additional_fields['order_comments']['enabled']){ |
| 119 | unset($fields['order']['order_comments']); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if(isset($fields['account']) && is_array($fields['account'])){ |
| 124 | foreach( $fields['account'] as $name => $values ) { |
| 125 | $fields['account'][ $name ]['label'] = __($fields['account'][ $name ]['label'], 'woocommerce'); |
| 126 | $fields['account'][ $name ]['placeholder'] = __($fields['account'][ $name ]['placeholder'], 'woocommerce'); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if(isset($fields['order']) && is_array($fields['order'])){ |
| 131 | foreach( $fields['order'] as $name => $values ) { |
| 132 | if (isset($values['enabled']) && $values['enabled'] == false ) { |
| 133 | unset( $fields['order'][ $name ] ); |
| 134 | }else{ |
| 135 | $fields['order'][ $name ]['label'] = __($fields['order'][ $name ]['label'], 'woocommerce'); |
| 136 | $fields['order'][ $name ]['placeholder'] = __($fields['order'][ $name ]['placeholder'], 'woocommerce'); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return $fields; |
| 142 | } |
| 143 | add_filter('woocommerce_checkout_fields', 'thwcfd_checkout_fields_lite', 1000); |
| 144 | |
| 145 | /** |
| 146 | * checkout_fields_modify_fields function. |
| 147 | * |
| 148 | * @param mixed $data |
| 149 | * @param mixed $old |
| 150 | */ |
| 151 | function thwcfd_prepare_checkout_fields_lite( $data, $old_fields ) { |
| 152 | global $WC_Checkout_Field_Editor; |
| 153 | |
| 154 | if( empty( $data ) ) { |
| 155 | return $old_fields; |
| 156 | |
| 157 | }else { |
| 158 | $fields = $data; |
| 159 | foreach( $fields as $name => $values ) { |
| 160 | // enabled |
| 161 | if ( $values['enabled'] == false ) { |
| 162 | unset( $fields[ $name ] ); |
| 163 | } |
| 164 | |
| 165 | // Replace locale field properties so they are unchanged |
| 166 | if ( in_array( $name, array( |
| 167 | 'billing_country', 'billing_state', 'billing_city', 'billing_postcode', |
| 168 | 'shipping_country', 'shipping_state', 'shipping_city', 'shipping_postcode', |
| 169 | 'order_comments' |
| 170 | ) ) ) { |
| 171 | if ( isset( $fields[ $name ] ) ) { |
| 172 | $fields[ $name ] = $old_fields[ $name ]; |
| 173 | $fields[ $name ]['label'] = ! empty( $data[ $name ]['label'] ) ? $data[ $name ]['label'] : $old_fields[ $name ]['label']; |
| 174 | |
| 175 | if ( ! empty( $data[ $name ]['placeholder'] ) ) { |
| 176 | $fields[ $name ]['placeholder'] = $data[ $name ]['placeholder']; |
| 177 | |
| 178 | } elseif ( ! empty( $old_fields[ $name ]['placeholder'] ) ) { |
| 179 | $fields[ $name ]['placeholder'] = $old_fields[ $name ]['placeholder']; |
| 180 | |
| 181 | } else { |
| 182 | $fields[ $name ]['placeholder'] = ''; |
| 183 | } |
| 184 | |
| 185 | $fields[ $name ]['class'] = $data[ $name ]['class']; |
| 186 | $fields[ $name ]['clear'] = $data[ $name ]['clear']; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if(isset($fields[$name])){ |
| 191 | $fields[ $name ]['label'] = __($fields[ $name ]['label'], 'woocommerce'); |
| 192 | $fields[ $name ]['placeholder'] = __($fields[ $name ]['placeholder'], 'woocommerce'); |
| 193 | } |
| 194 | } |
| 195 | return $fields; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * wc_checkout_fields_validation function. |
| 201 | * |
| 202 | * @param mixed $posted |
| 203 | */ |
| 204 | function thwcfd_checkout_fields_validation_lite($posted){ |
| 205 | foreach(WC()->checkout->checkout_fields as $fieldset_key => $fieldset){ |
| 206 | |
| 207 | // Skip shipping if its not needed |
| 208 | if($fieldset_key === 'shipping' && (WC()->cart->ship_to_billing_address_only() || !empty($posted['shiptobilling']) || |
| 209 | (!WC()->cart->needs_shipping() && get_option('woocommerce_require_shipping_address') === 'no'))){ |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | foreach($fieldset as $key => $field){ |
| 214 | if(!empty($field['validate']) && is_array($field['validate']) && !empty($posted[$key])){ |
| 215 | foreach($field['validate'] as $rule){ |
| 216 | switch($rule) { |
| 217 | case 'number' : |
| 218 | if(!is_numeric($posted[$key])){ |
| 219 | if(defined('WC_VERSION') && version_compare(WC_VERSION, '2.3.0', '>=')){ |
| 220 | wc_add_notice('<strong>'. $field['label'] .'</strong> '. sprintf(__('(%s) is not a valid number.', 'woocommerce'), $posted[$key]), 'error'); |
| 221 | } else { |
| 222 | WC()->add_error('<strong>'. $field['label'] .'</strong> '. sprintf(__('(%s) is not a valid number.', 'woocommerce'), $posted[$key])); |
| 223 | } |
| 224 | } |
| 225 | break; |
| 226 | case 'email' : |
| 227 | if(!is_email($posted[$key])){ |
| 228 | if(defined('WC_VERSION') && version_compare(WC_VERSION, '2.3.0', '<')){ |
| 229 | WC()->add_error('<strong>'. $field['label'] .'</strong> '. sprintf(__('(%s) is not a valid email address.', 'woocommerce'), $posted[$key])); |
| 230 | } |
| 231 | } |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | add_action('woocommerce_after_checkout_validation', 'thwcfd_checkout_fields_validation_lite'); |
| 240 | |
| 241 | /** |
| 242 | * Display custom fields in emails |
| 243 | * |
| 244 | * @param array $keys |
| 245 | * @return array |
| 246 | */ |
| 247 | function thwcfd_display_custom_fields_in_emails_lite($keys){ |
| 248 | $custom_keys = array(); |
| 249 | $fields = array_merge(WC_Checkout_Field_Editor::get_fields('billing'), WC_Checkout_Field_Editor::get_fields('shipping'), |
| 250 | WC_Checkout_Field_Editor::get_fields('additional')); |
| 251 | |
| 252 | // Loop through all custom fields to see if it should be added |
| 253 | foreach( $fields as $name => $options ) { |
| 254 | if(isset($options['show_in_email']) && $options['show_in_email']){ |
| 255 | $custom_keys[ esc_attr( $options['label'] ) ] = esc_attr( $name ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return array_merge( $keys, $custom_keys ); |
| 260 | } |
| 261 | add_filter('woocommerce_email_order_meta_keys', 'thwcfd_display_custom_fields_in_emails_lite', 10, 1); |
| 262 | |
| 263 | /** |
| 264 | * Display custom checkout fields on view order pages |
| 265 | * |
| 266 | * @param object $order |
| 267 | */ |
| 268 | function thwcfd_order_details_after_customer_details_lite($order){ |
| 269 | $order_id = $order->id; |
| 270 | |
| 271 | $fields = array(); |
| 272 | if(!wc_ship_to_billing_address_only() && $order->needs_shipping_address()){ |
| 273 | $fields = array_merge(WC_Checkout_Field_Editor::get_fields('billing'), WC_Checkout_Field_Editor::get_fields('shipping'), |
| 274 | WC_Checkout_Field_Editor::get_fields('additional')); |
| 275 | }else{ |
| 276 | $fields = array_merge(WC_Checkout_Field_Editor::get_fields('billing'), WC_Checkout_Field_Editor::get_fields('additional')); |
| 277 | } |
| 278 | |
| 279 | $found = false; |
| 280 | $html = ''; |
| 281 | |
| 282 | // Loop through all custom fields to see if it should be added |
| 283 | foreach($fields as $name => $options){ |
| 284 | $enabled = (isset($options['enabled']) && $options['enabled'] == false) ? false : true; |
| 285 | $is_custom_field = (isset($options['custom']) && $options['custom'] == true) ? true : false; |
| 286 | |
| 287 | if(isset($options['show_in_order']) && $options['show_in_order'] && $enabled && $is_custom_field){ |
| 288 | $found = true; |
| 289 | $html .= '<dt>' . esc_attr( $options['label'] ) . ':</dt>'; |
| 290 | $html .= '<dd>' . get_post_meta( $order_id, $name, true ) . '</dd>'; |
| 291 | } |
| 292 | } |
| 293 | if($found){ |
| 294 | echo '<dl>'. $html .'</dl>'; |
| 295 | } |
| 296 | } |
| 297 | add_action('woocommerce_order_details_after_customer_details', 'thwcfd_order_details_after_customer_details_lite', 20, 1); |
| 298 | |
| 299 | } |
| 300 |