Display custom checkbox field state on WooCommerce thankyou page

Viewed 201

Based on my previous question + the answer to the question

How to save from WooCommerce checkout a custom checkbox field state?

I have an additional checkbox on WooCommerce checkout page, but when the order is complete I don't see the additional field on the thankyou page. My checkbox save value 1 or 0 in meta_data when checkbox is checked or unchecked

$order->update_meta_data( 'ritiro_sede', $value );

I would like to see word SI if checkbox is checked and word NO if checkbox is unchecked, but this function not work:

Does anyone have any idea what might be wrong?

add_action( 'woocommerce_thankyou', 'my_custom_checkout_field_display_order_data_ritiro_sede', 10, 1 );
add_action( 'woocommerce_view_order', 'my_custom_checkout_field_display_order_data_ritiro_sede', 10, 1 );
function my_custom_checkout_field_display_order_data_ritiro_sede( $order ){ 
    $ritiro_sede = get_post_meta( $order_id, 'ritiro_sede', true );
    if( $ritiro_sede == 1 )
        echo '<p><strong>Ritiro In sede: </strong> <span style="color:red;">SI</span></p>';
    if( $ritiro_sede == 0 )
        echo '<p><strong>Ritiro In sede: </strong> <span style="color:red;">NO</span></p>';
    //echo '<p>'. __('Ritiro in Sede: ') . get_post_meta($order->get_id(), 'ritiro_sede', true .'</p');
    //echo '<p>'. __('Ritiro in Sede: ','theme_name') . get_value($order->get_the_id(), 'ritiro_sede', true .'</p>');       
}
1 Answers

1) The first mistake you will make is using $order as argument, while this should be the $order_id

So this

function my_custom_checkout_field_display_order_data_ritiro_sede( $order ){ 

Becomes

function my_custom_checkout_field_display_order_data_ritiro_sede( $order_id ){

2) The second mistake is that you are using $order_id, while it is not defined

$ritiro_sede = get_post_meta( $order_id, 'ritiro_sede', true );

This problem is now automatically solved by the adjustment from step 1


So to answer your question, you get:

function my_custom_checkout_field_display_order_data_ritiro_sede( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    
    // Get meta
    $ritiro_sede = $order->get_meta('ritiro_sede');
    
    // Compare
    if ( $ritiro_sede == 1 ) {
        echo '<p><strong>Ritiro In sede: </strong> <span style="color:green;">SI</span></p>';
    } else {
        echo '<p><strong>Ritiro In sede: </strong> <span style="color:red;">NO</span></p>';
    }       
}
add_action( 'woocommerce_thankyou', 'my_custom_checkout_field_display_order_data_ritiro_sede', 10, 1 );
add_action( 'woocommerce_view_order', 'my_custom_checkout_field_display_order_data_ritiro_sede', 10, 1 );
Related