Woocommerce Admin Order Details - Show custom data on order details page

前端 未结 1 1148
野性不改
野性不改 2021-02-04 09:15

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 o

相关标签:
1条回答
  • 2021-02-04 09:51

    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;
    }
    
    0 讨论(0)
提交回复
热议问题