Woocommerce管理订单详细信息 - 在订单详细信息页面上显示自定义数据 [英] Woocommerce Admin Order Details - Show custom data on order details page

查看:4556
本文介绍了Woocommerce管理订单详细信息 - 在订单详细信息页面上显示自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索并尝试2天没有成功,请帮助。



我要过滤woocommerce订单,从db添加其他详细信息到订单详情页面基于产品属性,但我不能找到正确的woocommerce行动/过滤器挂钩这个任务。
这里假设我有变量 $ is_customized = false ;



如果 $ is_customized == true ,那么我需要从数据库添加自定义数据到订单详细页面。 p>

注意:我不想添加其他元框,而是要更改订单明细表:




  • 使用存储在数据库中的图片替换默认的产品图片

  • 在产品下面添加包含自定义属性的div



我在变量中包含了所有这些值,但我不知道应该使用哪个操作。 p>

我附上了一张图片作澄清。



hook:

  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 是一个开始,但它需要一些自定义条件逻辑, t想过滤缩略图无处不在,但只有在订单。



编辑:目前我可以做的最好的过滤项目缩略图:

  add_filter('get_post_metadata','so_32457241_order_thumbnail',10,4); 
函数so_32457241_order_thumbnail($ value,$ post_id,$ meta_key,$ single){
//我们要将实际的_thumbnail_id传递给过滤器,因此需要递归
static $ is_recursing = false;
//如果我们不是递归,并且它是一个缩略图ID,只有过滤器
if(!$ is_recursing& $ meta_key ==='_thumbnail_id'){
$ is_recursing = true; //当调用get_post_thumbnail_id()时阻止这个条件
$ value = get_post_thumbnail_id($ post_id);
$ is_recursing = false;
$ value = apply_filters('post_thumbnail_id',$ value,$ post_id); // 好极了!
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'){
//这会得到shop_order $ post对象
global $ post;

//没有真正的*好*方法检查post项目,但可能保存
//在顺序meta中的某种数组
$ id = 68;
}
}
return $ id;
}


I'm searching and trying it for 2 days with no success, please help.

I want to filter woocommerce orders to add additional details from db to order details page based on product attribute but I can't find the right woocommerce action/filter hook for this task. Here suppose I've variable $is_customized = false;

If $is_customized == true then I need to add custom data from database to orders detail page.

NOTE: I don't want to add additional meta box instead I want to change order detail table for:

  • Replacing the default Product image with the image stored in database and,
  • Adding a div containing custom attributes below product name.

I've all these values in my variables but I can't figure out which action hook should I use.

I've attached an image for clarification.

Just need to know if I can change / filter these order results and how ?

I appreciate for your time and help. Thanks

解决方案

Here's a start on how to display some extra data on the woocommerce_before_order_itemmeta hook:

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

I don't know how you are saving your data, so I can't make more a more precise suggestion. Keep in mind that immediately following that hook, anything you've saved as order item meta will automatically display.

Filtering the image is more difficult. I've found this gist as a start, but it requires some custom conditional logic as you don't want to filter the thumbnail everywhere, but only in orders.

Edit: Currently the best I can do for filtering the item thumbnails:

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

这篇关于Woocommerce管理订单详细信息 - 在订单详细信息页面上显示自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆