functions - How to display custom option field in woocommerce orders in admin panel?

admin2025-01-08  6

Actually this question has already answered as i already checked but i am unable to comment on that question as i am using the same code but in my scenario i am using options field and value is not reflecting in admin panel below is the code that i tried

    //*Multiple Options Php - Select Occasion*/

add_filter( 'woocommerce_checkout_fields' , 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $fields ) { 
     $fields['billing']['myfield'] = array(
        'type'      => 'select',
        'id' => 'myfield',
        'placeholder'     => __('Select Occasion', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => false,
        'options'       => [
            'key1'  => __('Birthday', 'woocommerce'),
            'key2'  => __('Anniversary', 'woocommerce'),
            'key3'  => __('Congratulations', 'woocommerce'),
            'key4'  => __('Sorry', 'woocommerce'),
            'key5'  => __('Good Luck', 'woocommerce'),
            'key6'  => __('Thank You', 'woocommerce'),
            'key7'  => __('Get Well Soon', 'woocommerce'),
            'key8'  => __('Love', 'woocommerce'),
        ]
    );
    return $fields;
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'edit_woocommerce_checkout_page', 10, 1 );

function edit_woocommerce_checkout_page($order){
    global $post_id;
    $order = new WC_Order( $post_id );
    echo '<p><strong>'.__('Occasion').':</strong> ' . get_post_meta($order->get_id(), '_myfield', true ) . '</p>';
}

Actually this question has already answered as i already checked but i am unable to comment on that question as i am using the same code but in my scenario i am using options field and value is not reflecting in admin panel below is the code that i tried

    //*Multiple Options Php - Select Occasion*/

add_filter( 'woocommerce_checkout_fields' , 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $fields ) { 
     $fields['billing']['myfield'] = array(
        'type'      => 'select',
        'id' => 'myfield',
        'placeholder'     => __('Select Occasion', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => false,
        'options'       => [
            'key1'  => __('Birthday', 'woocommerce'),
            'key2'  => __('Anniversary', 'woocommerce'),
            'key3'  => __('Congratulations', 'woocommerce'),
            'key4'  => __('Sorry', 'woocommerce'),
            'key5'  => __('Good Luck', 'woocommerce'),
            'key6'  => __('Thank You', 'woocommerce'),
            'key7'  => __('Get Well Soon', 'woocommerce'),
            'key8'  => __('Love', 'woocommerce'),
        ]
    );
    return $fields;
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'edit_woocommerce_checkout_page', 10, 1 );

function edit_woocommerce_checkout_page($order){
    global $post_id;
    $order = new WC_Order( $post_id );
    echo '<p><strong>'.__('Occasion').':</strong> ' . get_post_meta($order->get_id(), '_myfield', true ) . '</p>';
}

Share Improve this question edited Jun 29, 2020 at 5:46 Ivan Shatsky 8901 gold badge7 silver badges12 bronze badges asked Jun 28, 2020 at 22:15 piyush arorapiyush arora 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You have forgot to save custom meta field data so check it below code I have done it. Reference

add_filter( 'woocommerce_checkout_fields' , 'occasion_checkout_field_update_order_meta' );
function occasion_checkout_field_update_order_meta( $fields ) { 
$fields['billing']['occasion'] = array( 'type' => 'select', 
    'id' => 'occasion', 
    'placeholder' => __('Select Occasion', 'woocommerce'), 
    'required' => false, 
    'class' => array('form-row-wide'), 
    'clear' => false, 
    'options' => array( 
        'Birthday' => __('Birthday', 'woocommerce'), 
        'Anniversary' => __('Anniversary', 'woocommerce'), 
        'Congratulations' => __('Congratulations', 'woocommerce'), 
        'Sorry' => __('Sorry', 'woocommerce'), 
        'Good Luck' => __('Good Luck', 'woocommerce'), 
        'Thank You' => __('Thank You', 'woocommerce'), 
        'Get Well Soon' => __('Get Well Soon', 'woocommerce'), 
        'Love' => __('Love', 'woocommerce')
    )
); 
return $fields; 
}
add_action( 'woocommerce_checkout_update_order_meta', 'occasion_checkout_update_order_meta' );
function occasion_checkout_update_order_meta( $order_id ){
    if( !empty( $_POST['occasion'] ) )
        update_post_meta( $order_id, '_occasion', sanitize_text_field( $_POST['occasion'] ) );
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'occasion_edit_woocommerce_checkout_page', 10, 1 );
function occasion_edit_woocommerce_checkout_page($order){ 
    echo __('Occasion').': '.get_post_meta($order->id, '_occasion', true ); 
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265751a1066.html

最新回复(0)