Created
May 14, 2015 14:59
-
-
Save hslaszlo/b741049879e075e1e8f9 to your computer and use it in GitHub Desktop.
Search by order item SKU or ID in Woocommerce Orders Admin page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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')); | |
}); |
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' );
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this code, how I can search product with SKU?