• Resolved 89design

    (@89design)


    I created a custom checkout field for WooCommerce in my functions.php file where users can select an exam date. These dates are loaded from a custom post type. I used the following code:

    add_action( 'woocommerce_before_order_notes', 'cinses_add_custom_checkout_field' );
          
        function cinses_add_custom_checkout_field( $checkout ) { 
          if(woo_in_cart(169)) {
            $saved_exam_date_practice = $current_user->exam_date_practice;
          
            $args = array(
              'post_type' => 'practice-data',
              'posts_per_page' => -1,
            );
            $query = new WP_Query($args);
    
            // Set variable (array)
            $options = array();
    
            if ( $query->have_posts() ) :
              while ( $query->have_posts() ) : $query->the_post();
                $options[get_the_ID()] = get_field('practice-exam-city') . ' ' . get_field('practice-exam-date');
              endwhile;
              wp_reset_postdata();
            endif;
             
            woocommerce_form_field( 'exam_date_practice', array(        
              'type' => 'select',        
              'label' => __( 'Datum praktijk examen' ),
              'options' => $options,
              'required' => true,        
              'default' => $saved_exam_date_practice,        
            ), $checkout->get_value( 'exam_date_practice' ) ); 
          }
        }

    And:

    add_action( 'woocommerce_checkout_update_order_meta', 'cinses_save_new_checkout_field' );
          
        function cinses_save_new_checkout_field( $order_id ) { 
    
            if ( $_POST['exam_date_practice'] ) update_post_meta( $order_id, 'exam_date_practice', esc_attr( $_POST['exam_date_practice'] ) );
    
        }
          
        add_action( 'woocommerce_admin_order_data_after_billing_address', 'cinses_show_new_checkout_field_order', 10, 1 );
           
        function cinses_show_new_checkout_field_order( $order ) {    
          $order_id = $order->get_id();
          
            if ( get_post_meta( $order_id, 'exam_date_practice', true ) ) echo '<strong>Datum praktijk examen:</strong> ' . get_post_meta( $order_id, 'exam_date_practice', true ) . '
    ';
          
        }
         
        add_action( 'woocommerce_email_after_order_table', 'cinses_show_new_checkout_field_emails', 20, 4 );
          
        function cinses_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
          
            if ( get_post_meta( $order->get_id(), 'exam_date_practice', true ) ) echo '<strong>Datum praktijk examen:</strong> ' . get_post_meta( $order->get_name(), 'exam_date_practice', true ) . '
    ';
          
        }

    Unfortunately this code shows me an ID in the WP admin and confirmation e-mails. How can I display the full date as text?

    What I get: “Exam date: 238”
    What I want: “Exam date: March 3, 2022”

    Thanks for your help in advance!`

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display custom checkout field text instead of an id’ is closed to new replies.