countries->get_base_country(); $options = []; if ( $base_country === 'CO' ) { $options = [ '' => __( 'Selecciona un tipo', 'alegra-invoicing-for-woocommerce' ), 'RC' => __( 'Registro civil', 'alegra-invoicing-for-woocommerce' ), 'TI' => __( 'Tarjeta de identidad', 'alegra-invoicing-for-woocommerce' ), 'CC' => __( 'Cédula de ciudadanía', 'alegra-invoicing-for-woocommerce' ), 'TE' => __( 'Tarjeta de extranjería', 'alegra-invoicing-for-woocommerce' ), 'CE' => __( 'Cédula de extranjería', 'alegra-invoicing-for-woocommerce' ), 'NIT' => __( 'Número de identificación tributaria', 'alegra-invoicing-for-woocommerce' ), 'PP' => __( 'Pasaporte', 'alegra-invoicing-for-woocommerce' ), 'PEP' => __( 'Permiso especial de permanencia', 'alegra-invoicing-for-woocommerce' ), 'DIE' => __( 'Documento de identificación extranjero', 'alegra-invoicing-for-woocommerce' ), 'NUIP' => __( 'NUIP', 'alegra-invoicing-for-woocommerce' ), 'FOREIGN_NIT' => __( 'NIT de otro país', 'alegra-invoicing-for-woocommerce' ), ]; } elseif ( $base_country === 'MX' ) { $options = [ '' => __( 'Selecciona un tipo', 'alegra-invoicing-for-woocommerce' ), 'NATIONAL' => __( 'Nacional', 'alegra-invoicing-for-woocommerce' ), 'FOREIGN' => __( 'Extranjero', 'alegra-invoicing-for-woocommerce' ), ]; } else { $options = [ '' => __( 'No aplica', 'alegra-invoicing-for-woocommerce' ) ]; } if ( $format_for_blocks ) { $block_options = []; foreach ( $options as $value => $label ) { $block_options[] = [ 'label' => $label, 'value' => (string) $value ]; } return $block_options; } return $options; } public function register_fields() { $is_visible = get_option( 'alegra_fields_visible', 'yes' ) === 'yes'; if ( ! $is_visible ) { return; } $doc_label = get_option( 'alegra_document_label', __( 'Document Number / Tax ID', 'alegra-invoicing-for-woocommerce' ) ); $doc_required = get_option( 'alegra_document_required', 'yes' ) === 'yes'; $type_label = get_option( 'alegra_document_type_label', __( 'Document Type / Customer Type', 'alegra-invoicing-for-woocommerce' ) ); $type_required = get_option( 'alegra_document_type_required', 'yes' ) === 'yes'; $current_user_id = get_current_user_id(); $default_doc = $current_user_id ? get_user_meta( $current_user_id, 'alegra_document_number', true ) : ''; $default_type = $current_user_id ? get_user_meta( $current_user_id, 'alegra_document_type', true ) : ''; if ( function_exists( 'woocommerce_register_additional_checkout_field' ) ) { woocommerce_register_additional_checkout_field( [ 'id' => 'alegra/document_type', 'label' => $type_label, 'location' => 'contact', 'type' => 'select', 'options' => $this->get_document_type_options( true ), 'required' => $type_required, ] ); woocommerce_register_additional_checkout_field( [ 'id' => 'alegra/document_number', 'label' => $doc_label, 'location' => 'contact', 'type' => 'text', 'required' => $doc_required, ] ); } add_filter( 'woocommerce_checkout_fields', function( $fields ) use ( $doc_label, $doc_required, $type_label, $type_required, $default_doc, $default_type ) { return $this->add_classic_checkout_fields( $fields, $doc_label, $doc_required, $type_label, $type_required, $default_doc, $default_type ); } ); add_action( 'woocommerce_checkout_create_order', [ $this, 'save_classic_checkout_fields' ], 10, 2 ); } public function add_classic_checkout_fields( $fields, $doc_label, $doc_required, $type_label, $type_required, $default_doc, $default_type ) { $fields['billing']['alegra_document_type'] = [ 'type' => 'select', 'label' => $type_label, 'required' => $type_required, 'options' => $this->get_document_type_options( false ), 'class' => [ 'form-row-first' ], 'priority' => 25, 'default' => $default_type, ]; $fields['billing']['alegra_document_number'] = [ 'type' => 'text', 'label' => $doc_label, 'required' => $doc_required, 'class' => [ 'form-row-last' ], 'priority' => 26, 'default' => $default_doc, ]; return $fields; } public function save_classic_checkout_fields( $order, $data ) { if ( isset( $data['alegra_document_type'] ) ) { $posted_type = sanitize_text_field( $data['alegra_document_type'] ); $valid_types = array_keys( $this->get_document_type_options( false ) ); if ( in_array( $posted_type, $valid_types, true ) ) { $order->update_meta_data( 'alegra_document_type', $posted_type ); } } if ( isset( $data['alegra_document_number'] ) ) { $order->update_meta_data( 'alegra_document_number', sanitize_text_field( $data['alegra_document_number'] ) ); } } public function save_fields_to_user_profile( $customer_id, $posted ) { if ( ! $customer_id ) { return; } if ( isset( $posted['alegra_document_type'] ) ) { update_user_meta( $customer_id, 'alegra_document_type', sanitize_text_field( $posted['alegra_document_type'] ) ); } if ( isset( $posted['alegra_document_number'] ) ) { update_user_meta( $customer_id, 'alegra_document_number', sanitize_text_field( $posted['alegra_document_number'] ) ); } } }