14

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
  • 277
  • 1
  • 6
  • 19
  • Like any other custom post type columns are handled via [`manage $post type posts columns`](https://codex.wordpress.org/Plugin_API/Filter_Reference/manage_$post_type_posts_columns) and [`manage $post type posts custom column`](https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column). And in the case of orders the post type is `shop_order`. – helgatheviking Sep 08 '15 at 15:19
  • Nevermind, I just looked more closely at your screenshot and it isn't what I was thinking. That said, it is a metabox, so at worst you could remove it, and add a modified version in its stead. – helgatheviking Sep 08 '15 at 15:21
  • means there is no action/filter available to filter these results or customize it instead of rewriting the whole module ? – Haider Saeed Sep 08 '15 at 15:27
  • 1
    **manage_edit-shop_order_columns** and **manage_shop_order_posts_custom_column** these both add extra column to orders table but I want to add column in order edit page where the details of the specific order is listed. – Haider Saeed Sep 08 '15 at 15:29
  • 1
    You can see the hooks you have at your disposal in [`html-order-items.php`](https://github.com/woothemes/woocommerce/blob/master/includes/admin/meta-boxes/views/html-order-items.php) and [`html-order-item.php`](https://github.com/woothemes/woocommerce/blob/master/includes/admin/meta-boxes/views/html-order-item.php) – helgatheviking Sep 08 '15 at 16:16
  • okay, thanks for the links I'll see and update here whether it works or not. Thanks – Haider Saeed Sep 08 '15 at 16:18

1 Answers1

21

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;
}
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • 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. – Haider Saeed Sep 08 '15 at 16:43
  • 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. – helgatheviking Sep 08 '15 at 16:50
  • 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. – Haider Saeed Sep 08 '15 at 16:55
  • 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. – helgatheviking Sep 08 '15 at 17:04
  • 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. :) – Haider Saeed Sep 08 '15 at 17:11
  • Keep an eye on this [pull request](https://github.com/woothemes/woocommerce/pull/9081). If it is merged into WooCommerce core, it will be easy to do what you are asking. – helgatheviking Sep 08 '15 at 17:48
  • I've added that in my fav but isn't it effect to core functionality when updating wordpress ? – Haider Saeed Sep 09 '15 at 09:29
  • 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) – helgatheviking Sep 09 '15 at 13:02
  • 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 – Haider Saeed Sep 09 '15 at 14:56
  • 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 Sep 10 '15 at 14:51
  • 1
    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. – helgatheviking Sep 16 '15 at 01:16
  • The intro with the simple bacon example was so helpful, thank you – chrisbergr Mar 08 '19 at 15:14