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.
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 useWC_Data
get_id()
method.3) To get the Order ID from
WC_Order_Item_Product
object you will useWC_Order_Item
methodget_order_id()
.4) You need to add
is_admin()
in theif
statement to restrict the display in backend.