Woocommerce 管理员订单详细信息 - 在订单详细信息页面上显示自定义数据
Woocommerce Admin Order Details - Show custom data on order details page
我正在搜索并尝试了 2 天没有成功,请帮助。
我想过滤 woocommerce 订单以根据产品属性从 db 添加更多详细信息到订单详细信息页面,但我找不到适合此任务的 woocommerce action/filter 挂钩。
这里假设我有变量 $is_customized = false
;
如果 $is_customized == true
那么我需要将自定义数据从数据库添加到订单详细信息页面。
注意:我不想添加额外的元框,而是想更改订单详细信息 table 为:
- 用数据库中存储的图像替换默认的产品图像
并且,
- 在产品名称下方添加包含自定义属性的 div。
我的变量中有所有这些值,但我不知道应该使用哪个操作挂钩。
我附上了一张图片以供说明。
只需要知道我是否可以更改/过滤这些订单结果以及如何更改/过滤?
感谢您抽出时间提供帮助。
谢谢
下面是如何在 woocommerce_before_order_itemmeta
挂钩上显示一些额外数据的开始:
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
echo '<p>bacon</p>';
}
我不知道你是如何保存你的数据的,所以我不能给出更准确的建议。请记住,在该挂钩之后,您保存为订单项元的任何内容都会自动显示。
过滤图像比较困难。我发现这个 gist 作为一个开始,但它需要一些自定义条件逻辑,因为您不想在任何地方过滤缩略图,而只是按顺序过滤。
编辑: 目前我能做的最好的过滤项目缩略图:
add_filter( 'get_post_metadata', 'so_32457241_order_thumbnail', 10, 4 );
function so_32457241_order_thumbnail( $value, $post_id, $meta_key, $single ) {
// We want to pass the actual _thumbnail_id into the filter, so requires recursion
static $is_recursing = false;
// Only filter if we're not recursing and if it is a post thumbnail ID
if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
$is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
$value = get_post_thumbnail_id( $post_id );
$is_recursing = false;
$value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
if ( ! $single ) {
$value = array( $value );
}
}
return $value;
}
add_filter( 'post_thumbnail_id', 'so_custom_order_item_thumbnail', 10, 2 );
function so_custom_order_item_thumbnail( $id, $post_id ){
if( is_admin() ){
$screen = get_current_screen();
if( $screen->base == 'post' && $screen->post_type == 'shop_order' ){
// this gets you the shop_order $post object
global $post;
// no really *good* way to check post item, but could possibly save
// some kind of array in the order meta
$id = 68;
}
}
return $id;
}
我正在搜索并尝试了 2 天没有成功,请帮助。
我想过滤 woocommerce 订单以根据产品属性从 db 添加更多详细信息到订单详细信息页面,但我找不到适合此任务的 woocommerce action/filter 挂钩。
这里假设我有变量 $is_customized = false
;
如果 $is_customized == true
那么我需要将自定义数据从数据库添加到订单详细信息页面。
注意:我不想添加额外的元框,而是想更改订单详细信息 table 为:
- 用数据库中存储的图像替换默认的产品图像 并且,
- 在产品名称下方添加包含自定义属性的 div。
我的变量中有所有这些值,但我不知道应该使用哪个操作挂钩。
我附上了一张图片以供说明。
只需要知道我是否可以更改/过滤这些订单结果以及如何更改/过滤?
感谢您抽出时间提供帮助。 谢谢
下面是如何在 woocommerce_before_order_itemmeta
挂钩上显示一些额外数据的开始:
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
echo '<p>bacon</p>';
}
我不知道你是如何保存你的数据的,所以我不能给出更准确的建议。请记住,在该挂钩之后,您保存为订单项元的任何内容都会自动显示。
过滤图像比较困难。我发现这个 gist 作为一个开始,但它需要一些自定义条件逻辑,因为您不想在任何地方过滤缩略图,而只是按顺序过滤。
编辑: 目前我能做的最好的过滤项目缩略图:
add_filter( 'get_post_metadata', 'so_32457241_order_thumbnail', 10, 4 );
function so_32457241_order_thumbnail( $value, $post_id, $meta_key, $single ) {
// We want to pass the actual _thumbnail_id into the filter, so requires recursion
static $is_recursing = false;
// Only filter if we're not recursing and if it is a post thumbnail ID
if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
$is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
$value = get_post_thumbnail_id( $post_id );
$is_recursing = false;
$value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
if ( ! $single ) {
$value = array( $value );
}
}
return $value;
}
add_filter( 'post_thumbnail_id', 'so_custom_order_item_thumbnail', 10, 2 );
function so_custom_order_item_thumbnail( $id, $post_id ){
if( is_admin() ){
$screen = get_current_screen();
if( $screen->base == 'post' && $screen->post_type == 'shop_order' ){
// this gets you the shop_order $post object
global $post;
// no really *good* way to check post item, but could possibly save
// some kind of array in the order meta
$id = 68;
}
}
return $id;
}