Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Created May 14, 2015 14:59
Show Gist options
  • Save hslaszlo/b741049879e075e1e8f9 to your computer and use it in GitHub Desktop.
Save hslaszlo/b741049879e075e1e8f9 to your computer and use it in GitHub Desktop.
Search by order item SKU or ID in Woocommerce Orders Admin page
//Search by product SKU in Admin Woocommerce Orders
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$posts = get_posts(array('post_type' => 'shop_order'));
foreach ($posts as $post) {
$order_id = $post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach($items as $item) {
$product_id = $item['product_id'];
$search_sku = get_post_meta($product_id, "_sku", true);
add_post_meta($order_id, "_product_sku", $search_sku);
}
}
return array_merge($search_fields, array('_product_sku'));
});
@mstudioIL
Copy link

Found this code, how I can search product with SKU?

@dreamdeveloper
Copy link

Here is the code that should work, add 'post_status' => 'any' to get_posts

function dakr_shop_order_search_order_sku( $search_fields ) {
    $posts = get_posts( array( 'post_type' => 'shop_order', 'post_status'    => 'any' ) );

	foreach ( $posts as $post ) {
		$order_id = $post->ID;
		$order    = new WC_Order( $order_id );
		$items    = $order->get_items();

		foreach ( $items as $item ) {
			$product_id = $item[ 'product_id' ];
			$search_sku = get_post_meta( $product_id, "_sku", true );

			add_post_meta( $order_id, "_product_sku", $search_sku );
		}
	}

	return array_merge( $search_fields, array( '_product_sku' ) );
}

add_filter( 'woocommerce_shop_order_search_fields', 'dakr_shop_order_search_order_sku' );

@mstudioIL
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment