0
votes

Hi I am using Woocommerce version 3.2.6. We have some orders.

I want to add one extra details to orders when the product id is 123 in the order edit page in wordpress backend.

I wanto add this:

<a href="http://example.com/new-view/?id=<?php echo $order_id?;>">Click here to view this</a>

ie: We have order[order id =3723] , and the ordered item id is 123.

Then in http://example.com/wp-admin/post.php?post=3723&action=edit, I want to add the following link below the corresponding item details:

"<a href="http://example.com/new-view/?id=<?php echo $order_id?;>">Click here to view this</a>"

How we can do this ?

Which hook is suitable for this. Actually I am searching in https://docs.woocommerce.com/wc-apidocs/hook-docs.html.

And I found Class WC_Meta_Box_Order_Items. But i don't know how to use this.

2

2 Answers

4
votes

The correct code for WooCommerce version 3+ to add a custom link just after line items and only on backend is:

add_action( 'woocommerce_after_order_itemmeta', 'custom_link_after_order_itemmeta', 20, 3 );
function custom_link_after_order_itemmeta( $item_id, $item, $product ) {
    // Only for "line item" order items
    if( ! $item->is_type('line_item') ) return;

    // Only for backend and  for product ID 123
    if( $product->get_id() == 123 && is_admin() )
        echo '<a href="http://example.com/new-view/?id='.$item->get_order_id().'">'.__("Click here to view this").'</a>';
}

Tested and works

1) Important: Limit the code to order items "line item" type only, to avoid errors on other order items like "shipping", "fee", "discount"...

2) From the WC_Product object to get the product id you will use WC_Data get_id() method.

3) To get the Order ID from WC_Order_Item_Product object you will use WC_Order_Item method get_order_id().

4) You need to add is_admin() in the if statement to restrict the display in backend.

3
votes

Order Items Meta Box uses html-order-items.php to loop through Order Items which in turn uses html-order-item.php to display each item.

For your purpose you should look inside html-order-item.php for exact place where you would want to insert your code snippet.

I assume woocommerce_after_order_itemmeta action hook is ideal as it will show the link just below meta information of the item. (In case you want to display the link before item meta than use woocommerce_before_order_itemmeta.)

add_action( 'woocommerce_after_order_itemmeta', 'wp177780_order_item_view_link', 10, 3 );
function wp177780_order_item_view_link( $item_id, $item, $_product  ){
    if( 123 == $_product->id ) {
        echo "<a href='http://example.com/new-view/?id=" . $order->id . "'>Click here to view this</a>";
    }
}