How To Sort Order Items By Name In WooCommerce

WooCommerceHere’s a handy little snippet to let you sort order items by name in WooCommerce. I had this requirement from a client of mine that receives large amounts of products in their customer orders.

When they’re picking stock off their shelves as they process the order the products with the same SKU with multiple variations were being added to the order in the order that the customer added them to the cart.

It made it really hard to see when there was multiple items ordered (different variations) under the same SKU and they found themselves having to go back to the shelf they were just at to grab another one of the same product, causing a lot of wasted time.

This bit of code sorts the items in your order by name which helps group everything together.

function sortOrderItemsByName($items, $order) {
	return usort($items, 'compareItemByName');
}
 
function compareItemByName($a, $b) {
	return strcasecmp($a['name'],$b['name']);
}
 
add_filter('woocommerce_order_get_items', 'sortOrderItemsByName');

This code will sort order items globally by name. This means in the admin screens and in emails going to the admin and the customer emails as well.

Hope this helps someone else out there with similar requirements.

Josh Kohlbach

Josh is a software entrepreneur from Brisbane, Australia. He spends most of his time helping e-commerce store owners. This is his personal blog where he shares his thoughts and other tidbits on online business and life in general.

This Post Has 13 Comments

  1. Lowell

    Awesome thanks. Would love to see a post about sorting by custom field too.

    1. Josh Kohlbach

      I guess it should be possible if you extended the sorting function. Technically you could do any kind of calls you like against the item meta data, see the get_item_meta() function on the WC_Order class.

  2. Dirkjan

    Hi Josh. Sounds good. Just what I need. But where do I put the code (In functions.php it ruined my email, this came up empty…) Thanks in advance.

    1. Josh Kohlbach

      Hi Dirkjan,

      Yes in your functions.php is fine. Make sure you put it at the end of the file, before the closing php tag ?>. Sounds like you might have had a syntax error somewhere there.

  3. James

    Hey Josh,

    Good on ya for this snippet. I’m starting to work on my first WooCommerce plugin to create a drag & drop interface for orders items (came about originally because I had used WooCommerce as a proposal tool in a past project). I have a custom query (StackOverflow) which I would like to replace get_items() in the WC_Order class with.

    Figured that I’d give you a shout to see if you could put me in the right direction.

    Thanks in advance,
    James

    1. Josh Kohlbach

      Hey James,

      I think Tom (in your SO post) might on the right path with extending the WC_Order class. Is this something you’re creating a custom backend screen for?

      Otherwise you could just use the same filter described above to return your own list of items. I normally wouldn’t recommend doing this for numerous reasons; two db calls for one but also other plugins might be hooking into that and you returning a different list of items really late in the piece would mean their data changes were moot.

  4. Nick

    Hi Josh,

    Good snippet, thanks! I would really want to see this working on a custom field, but my many attempts have failed. Can you help me with a little more specific info?

    Would be great!

  5. Michael

    Hello,

    We are trying to sell travel packages with woocommerce.
    The shoppers will pick individual items from our shop.
    I wish to have a function in cart that the shopper can drag and drop the items, and re-arrange the sequence of the products, how that is possible?

    1. Josh Kohlbach

      Hi Michael,

      You’re asking in the wrong place my friend, this is a blog post, not a general support forum.

      Try asking here and you might find someone that has an answer to your question:

      https://wordpress.org/support/plugin/woocommerce

  6. Quickbrown

    Hi Josh,

    I got this error on latest version of woocommerce (2.2+)

    Warning: Missing argument 2 for sortOrderItemsByName() in mysite.com/wp-content/themes/my-theme/functions.php on line xxx

    Does the snippet supposed to work with this version as well?

    Thanks.

    1. Andrew Schultz

      You need to write the filter like this

      add_filter(‘woocommerce_order_get_items’, ‘sortOrderItemsByName’, 10, 2);

  7. Yvan

    Been banging my head against this because the above code _almost_ works on current (March 2017) Woocommerce… after fixing that usort doesn’t return the array ref any more and the add_filter needs more args… it exhibits some odd behaviour. Tracked it down to “usort” killing the keys in the associative array (I’m guessing it didn’t use to be an associative array.) Anyway, simple fix is simple… use “uasort”, seems to be working right for me:


    function sortOrderItemsByName($items, $order) {
    uasort($items, 'compareItemByName');
    return $items;
    }
    function compareItemByName($a, $b) {
    return strcasecmp($a['name'],$b['name']);
    }
    add_filter('woocommerce_order_get_items', 'sortOrderItemsByName', 10, 2);

    1. Yvan

      March 2018… eh… 🙂

Leave a Reply