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

24,313

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;
}
Share:
24,313
Haider Saeed
Author by

Haider Saeed

I'm a Full Stack Developer having more than 4 years of experience in the field of web design and development.

Updated on July 09, 2022

Comments

  • Haider Saeed
    Haider Saeed almost 2 years

    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.

    enter image description here

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

    I appreciate for your time and help. Thanks

  • Haider Saeed
    Haider Saeed over 8 years
    Thanks a lot, its the exact thing which I'm actually looking for. Can you please tell me how I can replace image as well ? And I also need a filter for quantity here. Means I don't want to display quantity if the condition is true.
  • helgatheviking
    helgatheviking over 8 years
    The more I look at it the more I think filtering the image will be very difficult and possibly requires some filters be added to WooCommerce. It is easy enough to do in general, but the get_post_thumbnail_id() function relies purely on the $post_id and there's currently no way that I can see to send the $order_item which is the version of the product that is unique that that particular order.
  • Haider Saeed
    Haider Saeed over 8 years
    yes, let me explain. Actually its a designing tool and every customer will design his own product and obviously the product image will be different each time. I've successfully added custom data and image from session to cart and then orders but I'm just stuck here. The product image's name is in db and i can get it via order id but how to override ? That's the main point.
  • helgatheviking
    helgatheviking over 8 years
    I understand your question. I built something years ago to combine WooCommerce with EZprints, which is the same concept you are describing. I'm telling you that it (to my knowledge) isn't currently possible. I've edited the answer to display what is the best I can do without modifying WooCommerce.
  • Haider Saeed
    Haider Saeed over 8 years
    Okay thanks for your time. I'm going to implement this. I'll update here if it works or not. Thanks again and thanks to SOF community. Hope I can give you more votes. :)
  • helgatheviking
    helgatheviking over 8 years
    Keep an eye on this pull request. If it is merged into WooCommerce core, it will be easy to do what you are asking.
  • Haider Saeed
    Haider Saeed over 8 years
    I've added that in my fav but isn't it effect to core functionality when updating wordpress ?
  • helgatheviking
    helgatheviking over 8 years
    I don't understand your question. If that is added into WooCommerce you'll be able to filter the admin thumbnail directly in one of the next updates of WooCommerce. (or sooner if you patched WooCommerce yourself knowing the change has been approved)
  • Haider Saeed
    Haider Saeed over 8 years
    No, its okay now. I've used the above method and its working fine. I just use post_thumbnail_html instead of post_thumbnail_id and got what I want. It' not effecting anything else and working absolutely correctly as required. Thanks
  • helgatheviking
    helgatheviking over 8 years
    I skipped filtering post_thumbnail_html because if there is no thumbnail then I think Woo skips to a default image. But if your image always has a thumbnail then post_thumbnail_html can also work.
  • helgatheviking
    helgatheviking over 8 years
    FWI- the filter that I proposed to WC has been merged in to core, so you could go that route in anticipation that it will eventually be official code.
  • chrisbergr
    chrisbergr about 5 years
    The intro with the simple bacon example was so helpful, thank you