Only one of my WooCommerce conditional product fields is passing through to admin on Order

Viewed 17

I have entered this code into my functions.php but only the second product field (302) is showing in test orders in the admin section. Is there a way to get the 2nd product (294)to pass to the admin with this implementation?

add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );

function woo_add_conditional_checkout_fields( $fields ) {

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = $cart_item['product_id'];

            if( $product_id == 294 ) {
                $fields['billing']['billing_field_' . $product_id] = array(
                    'label'        => __( 'Enter License Plate and State' ),
                    'required'  => true,
                    'class'     => array('form-row-wide'),
                    'clear'     => true
                );
            }

            if( $product_id == 302 ) {
                $fields['billing']['billing_field_' . $product_id] = array(
                    'label'        => __( 'Enter License Plate, State AND CITATION Number' ),
                    'required'  => true,
                    'class'     => array('form-row-wide'),
                    'clear'     => true
                );
            }
    }
    // Return checkout fields.
    return $fields;

}
1 Answers
/**
 * Display field value on the order edit page
 */
 
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('License Plate and State').':</strong> ' . get_post_meta( $order->get_id(), '_billing_field_294', true ) . '</p>';
    echo '<p><strong>'.__('License Plate, State AND CITATION Number').':</strong> ' . get_post_meta( $order->get_id(), '_billing_field_302', true ) . '</p>';
}

enter image description here

Related