在管理员中添加额外的详细信息 Woocommerce 订单编辑页面

Add extra details Woocommerce order edit pages in admin

您好,我使用的是 Woocommerce 3.2.6 版。我们有一些订单。

我想在 wordpress 后端的订单编辑页面中为 product id123 时的订单添加一个额外的详细信息。

我想添加这个:

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

即:我们有order[order id =3723],订购的商品id为123

然后在http://example.com/wp-admin/post.php?post=3723&action=edit中,我想在相应的item details下方添加如下link:

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

我们如何做到这一点?

哪个钩子适合这个。实际上我在 https://docs.woocommerce.com/wc-apidocs/hook-docs.html 中搜索。

我找到了 Class WC_Meta_Box_Order_Items。但是我不知道怎么用。

Order Items Meta Box 使用 html-order-items.php 遍历 Order Items,后者又使用 html-order-item.php 显示每个项目。

为了您的目的,您应该在 html-order-item.php 内部查看您想要插入代码片段的确切位置。

我认为 woocommerce_after_order_itemmeta 动作挂钩是理想的,因为它会在项目的元信息下方显示 link。 (如果您想在项目元之前显示 link 而不是使用 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>";
    }
}

WooCommerce 版本 3+ 的正确代码 在订单项之后且仅在后端添加自定义 link 是:

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>';
}

已测试并有效

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.