WooCommerce: Sort Order Items by Name, SKU, Total, Quantity

By default, WooCommerce displays order items in the same order customers added them to the cart. That might work fine in most cases, but for many store owners, it’s more useful to have a consistent and logical sequence instead — especially when reviewing orders in the admin, generating invoices, or printing packing slips.

With a simple filter, you can change the sorting behavior of order items everywhere — on the Thank You page, in the admin order view, in customer accounts, and in any plugin that uses the same data source.

In this tutorial, we’ll go through a handy snippet that reorders WooCommerce order items using the woocommerce_order_get_items filter. You’ll see how to sort items alphabetically, by SKU, by quantity, by total value, or even by product category, depending on your specific workflow or fulfillment preferences.

In this screenshot, I’ve applied alphabetical sorting as per the first snippet below. Order items are now rearranged from A to Z!

PHP Snippet 1: Sort WooCommerce Order Items Alphabetically

/**
 * @snippet       Sort Products Alphabetically @ WooCommerce Order
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 10
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_order_get_items', 'bbloomer_sort_order_items_az', 9999, 2 );

function bbloomer_sort_order_items_az( $items, $order ) {
    uasort( $items, function( $a, $b ) {
        $nameA = $a->get_name();
        $nameB = $b->get_name();
        return strcasecmp( $nameA, $nameB ); // case-insensitive
    });
    return $items;
}

Line 1:
add_filter( 'woocommerce_order_get_items', ___
This tells WooCommerce: “Before returning the list of order items, run this custom function.”

Inside the function:
uasort() sorts the $items array while keeping the same keys (so WooCommerce still knows which item is which).

Each $a and $b represents an order item — for example, “T-shirt” or “Mug.”

$a->get_name() / $b->get_name():
These methods get the product names for comparison.

strcasecmp():
This built-in PHP function compares two strings alphabetically, ignoring case (so “apple” and “Apple” are treated the same).

Return values:

  • If $a comes before $b, the function returns a negative number.
  • If they’re equal, it returns 0.
  • If $a comes after $b, it returns a positive number.

That’s how uasort() decides the order.

Return $items:
After sorting, the modified $items array is returned to WooCommerce, which then displays the products alphabetically anywhere order items appear — on the Thank You page, in the admin, in customer accounts, and in plugins that rely on this same filter.

PHP Snippet 2: Sort WooCommerce Order Items by Quantity (most ordered items first)

Useful for packing or summarizing larger orders.

uasort( $items, function( $a, $b ) {
    return $b->get_quantity() <=> $a->get_quantity();
});

PHP Snippet 3: Sort WooCommerce Order Items by Line Total (highest value first)

Great for accounting, invoices, or prioritizing high-value items.

uasort( $items, function( $a, $b ) {
    return $b->get_total() <=> $a->get_total();
});

PHP Snippet 4: Sort WooCommerce Order Items by Product SKU (alphanumeric order)

Ideal for fulfillment teams who pick/pack by SKU.

uasort( $items, function( $a, $b ) {
    $skuA = $a->get_product() ? $a->get_product()->get_sku() : '';
    $skuB = $b->get_product() ? $b->get_product()->get_sku() : '';
    return strcasecmp( $skuA, $skuB );
});

PHP Snippet 5: Sort WooCommerce Order Items by Product Category

Makes sense when you want grouped packaging or a clearer structure on invoices.

uasort( $items, function( $a, $b ) {
    $catA = $a->get_product() ? wp_get_post_terms( $a->get_product_id(), 'product_cat', ['fields' => 'names'] ) : [];
    $catB = $b->get_product() ? wp_get_post_terms( $b->get_product_id(), 'product_cat', ['fields' => 'names'] ) : [];
    $catA_name = $catA ? $catA[0] : '';
    $catB_name = $catB ? $catB[0] : '';
    return strcasecmp( $catA_name, $catB_name );
});

PHP Snippet 6: Sort WooCommerce Order Items by Product ID

Handy if your product catalog is built in a known sequence.

uasort( $items, function( $a, $b ) {
    return $a->get_product_id() <=> $b->get_product_id();
});

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Sort Order Items by Name, SKU, Total, Quantity

  1. This is very helpful.

    Could you suggest a solution for sorting by category & then product alphabetically?

    1. Try with this:

      add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
          uasort( $items, function( $a, $b ) {
              $productA = $a->get_product();
              $productB = $b->get_product();
      
              // Get the first category name for each product
              $catA = $productA ? wp_get_post_terms( $productA->get_id(), 'product_cat', ['fields' => 'names'] ) : [];
              $catB = $productB ? wp_get_post_terms( $productB->get_id(), 'product_cat', ['fields' => 'names'] ) : [];
      
              $catA_name = $catA ? $catA[0] : '';
              $catB_name = $catB ? $catB[0] : '';
      
              // First compare categories
              $category_compare = strcasecmp( $catA_name, $catB_name );
              if ( $category_compare !== 0 ) {
                  return $category_compare;
              }
      
              // If same category, compare product names
              return strcasecmp( $a->get_name(), $b->get_name() );
          });
          return $items;
      }, 9999, 2 );
      
Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *