id = 'checkout_custom_fields'; $this->short_desc = __( 'Checkout Custom Fields', 'woocommerce-jetpack' ); $this->desc = __( 'Add custom fields to the checkout page.', 'woocommerce-jetpack' ); $this->link_slug = 'woocommerce-checkout-custom-fields'; parent::__construct(); if ( $this->is_enabled() ) { add_filter( 'woocommerce_checkout_fields', array( $this, 'add_custom_checkout_fields' ), PHP_INT_MAX ); add_action( 'woocommerce_admin_billing_fields', array( $this, 'add_custom_billing_fields_to_admin_order_display' ), PHP_INT_MAX ); add_action( 'woocommerce_admin_shipping_fields', array( $this, 'add_custom_shipping_fields_to_admin_order_display' ), PHP_INT_MAX ); add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $this, 'add_custom_order_and_account_fields_to_admin_order_display' ), PHP_INT_MAX ); if ( 'yes' === get_option( 'wcj_checkout_custom_fields_add_to_order_received', 'yes' ) ) { add_action( 'woocommerce_order_details_after_order_table', array( $this, 'add_custom_fields_to_view_order_and_thankyou_pages' ), PHP_INT_MAX ); } add_action( 'woocommerce_email_after_order_table', array( $this, 'add_custom_fields_to_emails' ), PHP_INT_MAX, 2 ); add_filter( 'woo_ce_order_fields', array( $this, 'add_custom_fields_to_store_exporter' ) ); add_filter( 'woo_ce_order', array( $this, 'add_custom_fields_to_store_exporter_order' ), PHP_INT_MAX, 2 ); add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'update_custom_checkout_fields_order_meta' ) ); // add_action( 'woocommerce_order_formatted_shipping_address', array( $this, 'add_custom_shipping_fields_to_formatted_address' ), PHP_INT_MAX, 2 ); // add_filter( 'woocommerce_form_field_' . 'number', array( $this, 'woocommerce_form_field_type_number' ), PHP_INT_MAX, 4 ); add_filter( 'woocommerce_form_field_' . 'text', array( $this, 'woocommerce_form_field_type_number' ), PHP_INT_MAX, 4 ); add_filter( 'woocommerce_customer_meta_fields', array( $this, 'add_checkout_custom_fields_customer_meta_fields' ) ); for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); $the_key = 'wcj_checkout_field_' . $i; $the_name = $the_section . '_' . $the_key; add_filter( 'default_checkout_' . $the_name, array( $this, 'add_default_checkout_custom_fields' ), PHP_INT_MAX, 2 ); } } // select2 script add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_scripts' ) ); } } /** * maybe_enqueue_scripts. * * @version 3.6.0 * @since 3.2.0 */ function maybe_enqueue_scripts( $fields ) { if ( is_checkout() ) { $select2_fields = array(); for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i, 'no' ) ) { if ( 'select' === get_option( 'wcj_checkout_custom_field_type_' . $i, 'text' ) ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_select_select2_' . $i, 'no' ) ) { $select2_fields[] = array( 'field_id' => get_option( 'wcj_checkout_custom_field_section_' . $i, 'billing' ) . '_' . 'wcj_checkout_field_' . $i, 'minimumInputLength' => get_option( 'wcj_checkout_custom_field_select_select2_min_input_length' . $i, 0 ), 'maximumInputLength' => get_option( 'wcj_checkout_custom_field_select_select2_max_input_length' . $i, 0 ), ); } } } } if ( ! empty( $select2_fields ) ) { wp_enqueue_script( 'wcj-checkout-custom-fields', wcj_plugin_url() . '/includes/js/wcj-checkout-custom-fields.js', array( 'jquery' ), WCJ()->version, true ); wp_localize_script( 'wcj-checkout-custom-fields', 'wcj_checkout_custom_fields', array( 'select2_fields' => $select2_fields, ) ); } } } /** * add_checkout_custom_fields_customer_meta_fields. * * @version 3.2.4 * @since 2.4.5 */ function add_checkout_custom_fields_customer_meta_fields( $fields ) { for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { if ( 'no' === get_option( 'wcj_checkout_custom_field_customer_meta_fields_' . $i, 'yes' ) ) { continue; } $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); $the_key = 'wcj_checkout_field_' . $i; $the_name = $the_section . '_' . $the_key; $fields[ $the_section ]['fields'][ $the_name ] = array( 'label' => get_option( 'wcj_checkout_custom_field_label_' . $i ), 'description' => '', ); } } return $fields; } /** * add_default_checkout_custom_fields. * * @version 2.4.5 * @since 2.4.5 */ function add_default_checkout_custom_fields( $default_value, $field_key ) { if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); if ( $meta = get_user_meta( $current_user->ID, $field_key, true ) ) { return $meta; } } return $default_value; } /** * woocommerce_form_field_type_number. * * @version 2.3.0 * @since 2.3.0 */ function woocommerce_form_field_type_number( $field, $key, $args, $value ) { /* $args['input_class'] = array(); $args['maxlength'] = ''; $custom_attributes = array(); $field = ''; return $field; */ if ( isset( $args['custom_attributes']['display'] ) && 'number' === $args['custom_attributes']['display'] ) { $field = str_replace( ' $values ) { if ( false !== strpos( $key, 'wcj_checkout_field_' ) && isset( $values[0] ) ) { if ( false !== strpos( $key, '_label_' ) ) { continue; } $order->$key = isset( $values[0]['value'] ) ? $values[0]['value'] : $values[0]; } } return $order; } /** * add_custom_fields_to_store_exporter. */ function add_custom_fields_to_store_exporter( $fields ) { for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); $the_key = 'wcj_checkout_field_' . $i; $fields[] = array( 'name' => $the_section . '_' . $the_key, 'label' => get_option( 'wcj_checkout_custom_field_label_' . $i ), ); } } return $fields; } /** * add_custom_shipping_fields_to_formatted_address. */ /* function add_custom_shipping_fields_to_formatted_address( $fields, $order ) { for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { //if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); if ( 'shipping' === $the_section ) { $option_name = $the_section . '_' . 'wcj_checkout_field_' . $i; $fields[ $option_name ] = get_post_meta( wcj_get_order_id( $order ), '_' . $option_name, true ); } //} } return $fields; } */ /** * update_custom_checkout_fields_order_meta. * * @version 2.4.7 */ function update_custom_checkout_fields_order_meta( $order_id ) { for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); $the_type = get_option( 'wcj_checkout_custom_field_type_' . $i ); $option_name = $the_section . '_' . 'wcj_checkout_field_' . $i; $option_name_label = $the_section . '_' . 'wcj_checkout_field_label_' . $i; $option_name_type = $the_section . '_' . 'wcj_checkout_field_type_' . $i; if ( ! empty( $_POST[ $option_name ] ) || 'checkbox' === $the_type ) { update_post_meta( $order_id, '_' . $option_name_type, $the_type ); update_post_meta( $order_id, '_' . $option_name_label, get_option( 'wcj_checkout_custom_field_label_' . $i ) ); if ( 'checkbox' === $the_type ) { $the_value = ( isset( $_POST[ $option_name ] ) ) ? 1 : 0; update_post_meta( $order_id, '_' . $option_name, $the_value ); $option_name_checkbox_value = $the_section . '_' . 'wcj_checkout_field_checkbox_value_' . $i; $checkbox_value = ( 1 == $the_value ) ? get_option( 'wcj_checkout_custom_field_checkbox_yes_' . $i ) : get_option( 'wcj_checkout_custom_field_checkbox_no_' . $i ); update_post_meta( $order_id, '_' . $option_name_checkbox_value, $checkbox_value ); } elseif ( 'radio' === $the_type || 'select' === $the_type ) { update_post_meta( $order_id, '_' . $option_name, wc_clean( $_POST[ $option_name ] ) ); $option_name_values = $the_section . '_' . 'wcj_checkout_field_select_options_' . $i; $the_values = get_option( 'wcj_checkout_custom_field_select_options_' . $i ); update_post_meta( $order_id, '_' . $option_name_values, $the_values ); } else { update_post_meta( $order_id, '_' . $option_name, wc_clean( $_POST[ $option_name ] ) ); } } } } } /** * add_custom_fields_to_emails. * * @version 3.2.2 */ function add_custom_fields_to_emails( $order, $sent_to_admin ) { if ( ( $sent_to_admin && 'yes' === get_option( 'wcj_checkout_custom_fields_email_all_to_admin' ) ) || ( ! $sent_to_admin && 'yes' === get_option( 'wcj_checkout_custom_fields_email_all_to_customer' ) ) ) { $templates = array( 'before' => get_option( 'wcj_checkout_custom_fields_emails_template_before', '' ), 'field' => get_option( 'wcj_checkout_custom_fields_emails_template_field', '
%label%: %value%
' ), 'after' => get_option( 'wcj_checkout_custom_fields_emails_template_after', '' ), ); $this->add_custom_fields_to_order_display( $order, '', $templates ); } } /** * add_custom_fields_to_view_order_and_thankyou_pages. * * @version 3.2.2 * @since 3.2.2 */ function add_custom_fields_to_view_order_and_thankyou_pages( $order ) { $templates = array( 'before' => get_option( 'wcj_checkout_custom_fields_order_received_template_before', '' ), 'field' => get_option( 'wcj_checkout_custom_fields_order_received_template_field', '%label%: %value%
' ), 'after' => get_option( 'wcj_checkout_custom_fields_order_received_template_after', '' ), ); $this->add_custom_fields_to_order_display( $order, '', $templates ); } /** * add_custom_fields_to_order_display. * * @version 3.2.2 * @since 2.3.0 * @todo convert from before version 2.3.0 */ function add_custom_fields_to_order_display( $order, $section = '', $templates ) { $post_meta = get_post_meta( wcj_get_order_id( $order ) ); $final_output = ''; foreach( $post_meta as $key => $values ) { if ( false !== strpos( $key, 'wcj_checkout_field_' ) && isset( $values[0] ) ) { // Checking section (if set) if ( '' != $section ) { $the_section = strtok( $key, '_' ); if ( $section !== $the_section ) { continue; } } // Skipping unnecessary meta if ( false !== strpos( $key, '_label_' ) || false !== strpos( $key, '_type_' ) || false !== strpos( $key, '_checkbox_value_' ) || false !== strpos( $key, '_select_options_' ) ) { continue; } // Field label $label = ''; $the_label_key = str_replace( 'wcj_checkout_field_', 'wcj_checkout_field_label_', $key ); if ( isset( $post_meta[ $the_label_key ][0] ) ) { $label = $post_meta[ $the_label_key ][0]; } elseif ( is_array( $values[0] ) && isset( $values[0]['label'] ) ) { $label = $values[0]['label']; } // Field value $value = ''; $_value = ( is_array( $values[0] ) && isset( $values[0]['value'] ) ? $values[0]['value'] : $values[0] ); $type_key = str_replace( 'wcj_checkout_field_', 'wcj_checkout_field_type_', $key ); if ( isset( $post_meta[ $type_key ][0] ) && 'checkbox' === $post_meta[ $type_key ][0] ) { $checkbox_value_key = str_replace( 'wcj_checkout_field_', 'wcj_checkout_field_checkbox_value_', $key ); $value = ( isset( $post_meta[ $checkbox_value_key ][0] ) ? $post_meta[ $checkbox_value_key ][0] : $_value ); } elseif ( isset( $post_meta[ $type_key ][0] ) && ( 'radio' === $post_meta[ $type_key ][0] || 'select' === $post_meta[ $type_key ][0] ) ) { $select_values_key = str_replace( 'wcj_checkout_field_', 'wcj_checkout_field_select_options_', $key ); $select_values = ( isset( $post_meta[ $select_values_key ][0] ) ) ? $post_meta[ $select_values_key ][0] : ''; if ( ! empty( $select_values ) ) { $select_values_prepared = wcj_get_select_options( $select_values ); $value = ( isset( $select_values_prepared[ $_value ] ) ? $select_values_prepared[ $_value ] : $_value ); } else { $value = $_value; } } else { $value = $_value; } // Adding field to final output if ( '' != $label || '' != $value ) { $replaced_values = array( '%label%' => $label, '%value%' => $value, ); $final_output .= str_replace( array_keys( $replaced_values ), $replaced_values, $templates['field'] ); } } } // Outputting if ( '' != $final_output ) { echo $templates['before'] . $final_output . $templates['after']; } } /** * add_woocommerce_admin_fields. * * @version 2.4.7 * @todo Converting from before version 2.3.0: section? */ function add_woocommerce_admin_fields( $fields, $section ) { for ( $i = 1; $i <= apply_filters( 'booster_option', 1, get_option( 'wcj_checkout_custom_fields_total_number', 1 ) ); $i++ ) { if ( 'yes' === get_option( 'wcj_checkout_custom_field_enabled_' . $i ) ) { $the_section = get_option( 'wcj_checkout_custom_field_section_' . $i ); if ( $section != $the_section ) { continue; } $the_type = get_option( 'wcj_checkout_custom_field_type_' . $i ); if ( 'select' === $the_type ) { $the_class = 'first'; $options = wcj_get_select_options( get_option( 'wcj_checkout_custom_field_select_options_' . $i ) ); } elseif ( 'radio' === $the_type ) { $the_options = get_post_meta( get_the_ID(), '_' . $section . '_' . 'wcj_checkout_field_select_options_' . $i, true ); if ( ! empty( $the_options ) ) { $the_type = 'select'; $the_class = 'first'; $options = wcj_get_select_options( $the_options ); } else { $the_options = wcj_get_select_options( get_option( 'wcj_checkout_custom_field_select_options_' . $i ) ); if ( ! empty( $the_options ) ) { $the_type = 'select'; $the_class = 'first'; $options = $the_options; } else { $the_type = 'text'; $the_class = 'short'; } } } elseif ( 'country' === $the_type ) { $the_type = 'select'; $the_class = 'js_field-country select short'; $options = WC()->countries->get_allowed_countries(); } else { $the_type = 'text'; $the_class = 'short'; } $the_key = 'wcj_checkout_field_' . $i; $the_key_label = 'wcj_checkout_field_label_' . $i; $the_meta = get_post_meta( get_the_ID(), '_' . $section . '_' . $the_key, true ); if ( is_array( $the_meta ) ) { // Converting from before version 2.3.0 if ( isset( $the_meta['value'] ) ) update_post_meta( get_the_ID(), '_' . $section . '_' . $the_key, $the_meta['value'] ); if ( isset( $the_meta['label'] ) ) update_post_meta( get_the_ID(), '_' . $section . '_' . $the_key_label, $the_meta['label'] ); } $fields[ $the_key ] = array( 'type' => $the_type, 'label' => ( '' != get_post_meta( get_the_ID(), '_' . $section . '_' . $the_key_label, true ) ) ? get_post_meta( get_the_ID(), '_' . $section . '_' . $the_key_label, true ) : get_option( 'wcj_checkout_custom_field_label_' . $i ), 'show' => true, 'class' => $the_class, 'wrapper_class' => 'form-field-wide', ); if ( isset( $options ) ) { $fields[ $the_key ]['options'] = $options; } } } return $fields; } /** * add_custom_billing_fields_to_admin_order_display. */ function add_custom_billing_fields_to_admin_order_display( $fields ) { return $this->add_woocommerce_admin_fields( $fields, 'billing' ); } /** * add_custom_shipping_fields_to_admin_order_display. */ function add_custom_shipping_fields_to_admin_order_display( $fields ) { return $this->add_woocommerce_admin_fields( $fields, 'shipping' ); } /** * add_custom_order_and_account_fields_to_admin_order_display * * @version 3.2.2 */ function add_custom_order_and_account_fields_to_admin_order_display( $order ) { $templates = array( 'before' => '',
'field' => '%label%: %value%
',
'after' => '