Customize sorting for products within a customer Order

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Within a WooCommerce buyer Order, the products are being sorted / displayed in the way they where placed in the cart. Unfortunate WooCommerce does not provide any support through which such sorting can be changed.

woocommerce-sort-products-within-customer-order

The same for front side customer view, when order page is being viewed, the products appear the the same order as they where placed in the shopping cart.

woocommerce-sort-products-within-customer-order-front-view

The Advanced Post Types Order plugin can handle such sorting, through the easy to use drag & drop interface. First a sort need to be created, detailed instructions on how to add a new list can be found at Create sort list The sort should use conditionals to narrow down the sort appliance for only designated areas instead site-wide. If no conditionals are being set, the sorting also apply to shop archive or any other section the sort list order has been changed. Example of conditionals to make the sort to apply only on customer order page (front side) and admin order view

woocommerce-sort-products-within-customer-order-sort-list-conditionals

The order should be changed within Archive selection for the products to follow the customized sort.

Since the WooCommerce display the products within an order the way they where pushed to shopping cart, we need to modify this behavior through a custom code. This should be placed within theme function.php file or within a custom plugin

	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;
               
        }

Specific sort list can be included by specifying an ‘sort_id’ within the custom query arguments, fore more details see the Sample Usage on how to add such parameter.