Sabhay Sardana
Sabhay Sardana

Reputation: 886

Displaying an ACF field via `woocommerce_view_order`

I have created a custom field in my order with the ACF plugin. The field contains a tracking code and is stored under _tracking_code.

I am trying to display the tracking code via the woocommerce_view_order action hook.

add_action( 'woocommerce_view_order', 'order_page', 30 );
function order_page() {
    echo get_field('tracking_code'); // ACF Custom Field
}

enter image description here

Upvotes: 2

Views: 286

Answers (1)

Terminator-Barbapapa
Terminator-Barbapapa

Reputation: 3126

The woocommerce_view_order hook gives you the order ID to work with. So you could do something like this"

add_action( 'woocommerce_view_order', 'add_custom_tracking_field', 10, 1 );
function add_custom_tracking_field( $order_id ) {
    if ( $order = wc_get_order( $order_id ) ) {
        if ( !empty( $order->get_meta( 'tracking_code' ) ) ) {
            printf( 'Tracking code: %s', $order->get_meta( 'tracking_code' ) );
        }
    }
}

Upvotes: 2

Related Questions