![](/img/trans.png)
[英]Add a custom order note programmatically in Woocommerce admin order edit pages
[英]Add extra details Woocommerce order edit pages in admin
嗨,我正在使用Woocommerce版本3.2.6。 我們有一些訂單。
我想在wordpress后端的訂單編輯頁面中,當product id
為123
時向訂單添加一個額外的詳細信息。
我要添加此:
<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
,我想在相應的項目詳細信息下面添加以下鏈接:
"<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中搜索。
我發現了類WC_Meta_Box_Order_Items
。 但是我不知道該怎么用。
訂購商品 Meta Box使用html-order-items.php
遍歷訂購商品,而訂購商品又使用html-order-item.php
顯示每個商品。
為了您的目的,您應該在html-order-item.php
內查找要插入代碼段的確切位置。
我認為woocommerce_after_order_itemmeta
操作鈎是理想的,因為它將在項目的元信息下方顯示鏈接。 (如果您要在項目元數據之前顯示鏈接,而不是使用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+在訂單項后且僅在后端添加自定義鏈接的正確代碼是:
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) 重要提示:將代碼限制為僅訂購商品“訂單項”類型,以避免其他訂單商品(如“運費”,“費用”,“折扣”)出錯。
2)從
WC_Product
對象獲取產品ID,您將使用WC_Data
get_id()
方法。3)要從
WC_Order_Item_Product
對象獲取訂單ID ,您將使用WC_Order_Item
方法get_order_id()
。4)您需要在
if
語句中添加is_admin()
,以限制后端的顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.