Add order metadata to WooCommerce admin order overview
Asked Answered
R

3

7

enter image description here

I am developing a plugin for WooCommerce. I want to override the order details template of admin. i have read about on https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/ , but still I don't understand how to override the order detail template of admin. following is my code:

 if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
 if ( ! class_exists( 'Test' ) ) {
   load_plugin_textdomain( 'test', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
}
}
class Test {
public function __construct() {
add_action( 'init', array( $this, 'include_template_functions' ), 20 );
add_action( 'woocommerce_init', array( $this, 'woocommerce_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
do_action( 'woocommerce_admin_order_data_after_order_details', 'hello' );
}

public function hello() {
echo "order detail template has loaded";
}

public function include_template_functions() {
include( 'woocommerce-template.php' );
echo "template has loaded";
 }

  public function woocommerce_loaded() {
  }

public function plugins_loaded() {
 }
 }

$GLOBALS['wc_acme'] = new Test();

It's not calling the hook associated with woocommerce_admin_order_data_after_order_details.

Can anyone please suggest or share some example of editing the order details template editing via plugin. Please note that I am referring to the order detail template inside the administrator, where administrator can view the detail of any order from the list.

Runck answered 13/4, 2015 at 9:44 Comment(4)
I am not sure which template you are referring to. Could you screenshot it? And if possible screenshot what you are trying to add? Justin's tutorial is for overriding front-end WooCommerce templates. It doesn't look like you are following it at all so I might be able to help more with some clarification.Attentive
actually i have tried justin tutorial too, but i did not work.Runck
His tutorial works just fine for its purpose... which is override the front-end templates. Adding data to the screenshot you've posted is something else entirely. What specifically do you want to change? I can show you how to add extra data to the above screenshot.Attentive
I want to add extra section for the new data. actually i have an API on my own server that returns the geolocation and i want to show that geolocation before the order item section.Runck
A
21

From my tutorial on customizing WooCommerce checkout fields this is how you'd display some extra order meta data in the Order Details metabox:

// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){  ?>
    <div class="order_data_column">
        <h4><?php _e( 'Extra Details' ); ?></h4>
        <?php 
            echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
            echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

This assumes that you have collected the data on checkout and saved the data as post meta for the $order in question.

Attentive answered 13/4, 2015 at 10:51 Comment(5)
Thanks a million for the help.Runck
This is good answer . i am using the same code for seeing the extra custom billing field and the hook is 'woocommerce_admin_order_data_after_billing_address'. But it will only display the label and value . But how can i edit this details .Delacroix
What details? I would suggest you that you open your own question.Attentive
hey there @Attentive i'm trying to do the same thing but without any plugin i just have couple of custom fields which i want to display in the item section of adminAcademicism
@HabibRehman carefully read the tutorial I linked to. Then if you still have a problem, come back to SO and post a detailed question (a new question and not a reply to this thread) identifying what you are trying to do that isn't being covered by the above code.Attentive
P
1

You could use woocommerce_admin_order_data_after_billing_address action:

function order_phone_backend($order){
    echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->id, '_billing_phone_new', true ) . "</p><br>";
} 
add_action( 'woocommerce_admin_order_data_after_billing_address','order_phone_backend', 10, 1 );
Philippi answered 3/7, 2021 at 7:52 Comment(0)
A
0

The above code from @saeed is working fine. But from woocommerce v3 you need to change the $order->id to $order->get_id()

function order_phone_backend($order){
    echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->get_id(), '_billing_phone_new', true ) . "</p><br>";
} 
add_action( 'woocommerce_admin_order_data_after_billing_address','order_phone_backend', 10, 1 );
Ahead answered 25/9 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.