I use this function to sort Woocommerce order admin items by menu order but But products with variables do not display properly. And if there are several product with variables in the order, only one of them will be displayed.
edit: we have problem with multiple items of a product with different attributes:
item1: product A,variable a,attribute: red color, qty 12
item2: Product A, variable a, attribute: green color, qty 18
after sort it only shows :
item1: product A,variable a,attribute: red color, qty 12
In other words product items with same variation id have problem.
add_filter('woocommerce_order_get_items', 'custom_woocommerce_order_get_items', 10, 2);
function custom_woocommerce_order_get_items($items, $object)
{
//no need to reorder if less than 2 products
if(count($items) < 2)
return $items;
//create a list of products within the order
$products = array();
foreach($items as $key => $item)
{
$products[ $item['product_id'] ] = $key;
}
$sorted_items = array();
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array_keys($products)
);
$custom_query = new WP_Query($args);
while($custom_query->have_posts())
{
$custom_query->the_post();
$sorted_items[ $products[$post->ID] ] = $items[ $products[$post->ID] ];
}
//check for any left outside items
foreach($items as $key => $item)
{
if(isset($sorted_items[$key]))
$sorted_items[ $key ] = $item;
}
return $sorted_items;
}