在 Woocommerce 管理订单预览上显示自定义数据 [英] Display custom data on Woocommerce admin order preview
问题描述
我想在 Woocommerce 订单列表页面的预览订单末尾添加一些自定义数据.
I would like to add some custom data to the end of the preview order in Woocommerce order listing page.
为此,我尝试了woocommerce_admin_order_preview_end"这个钩子.但无法向该操作传递任何参数.
For that I have tried the hook 'woocommerce_admin_order_preview_end'. But no way to pass any arguments to that action.
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin( $order ){
//$order is empty here
}
有人对此有什么想法吗?我坚持这个.
Does anybody have an idea on this? I'm stuck on this.
推荐答案
您无法获取订单对象,因为它是通过 Ajax 加载特定数据的模板,并且 woocommerce_admin_order_preview_end
动作挂钩.
You can't get the order object as it's a template that loads specific data via Ajax and there is no arguments for woocommerce_admin_order_preview_end
action hook.
相反,过滤器钩子 woocommerce_admin_order_preview_get_order_details
将允许您首先添加一些自定义数据,您可以在 woocommerce_admin_order_preview_end
操作钩子中调用和显示这些数据.
Instead the filter hook woocommerce_admin_order_preview_get_order_details
will allow you first to add some custom data that you will be able to call and display it after in woocommerce_admin_order_preview_end
action hook.
代码:
// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $custom_value = $order->get_meta('_custom_meta_key') )
$data['custom_key'] = $custom_value; // <= Store the value in the data array.
return $data;
}
// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Value: {{data.custom_key}}</div><br>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
注意:如果需要,您也可以使用 woocommerce_admin_order_preview_start
钩子......
Note: You can also use
woocommerce_admin_order_preview_start
hook if needed…
这篇关于在 Woocommerce 管理订单预览上显示自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!