What I am trying to do is to be able to search by order item SKU or ID in the WooCommerce Orders Admin page.
What I have found/done till now, but with no success is the following at functions.php
file.
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_sku' );
function woocommerce_shop_order_search_sku( $search_fields ) {
$args = array( 'post_type' => 'shop_order' );
$orders = new WP_Query( $args );
if ( $orders->have_posts() ) {
while( $orders->have_posts() ) {
$post = $orders->the_post();
$order_id = get_the_ID();
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach( $items as $item ) {
$search_order_item_sku = wp_get_post_terms( $item['product_id'], 'search_sku' );
foreach( $search_order_item_sku as $search_sku ) {
add_post_meta( $order_id, "_search_sku", $search_sku->sku );
}
}
}
};
$search_fields[] = '_search_sku';
return $search_fields;
}
I suppose the issue is the value of $search_sku
at the line with the add_post_meta
.
I have also tried it with get_sku()
, $item['sku']
with no luck.