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.

- PHP Snippet 1: Sort WooCommerce Order Items Alphabetically
- PHP Snippet 2: Sort WooCommerce Order Items by Quantity (most ordered items first)
- PHP Snippet 3: Sort WooCommerce Order Items by Line Total (highest value first)
- PHP Snippet 4: Sort WooCommerce Order Items by Product SKU (alphanumeric order)
- PHP Snippet 5: Sort WooCommerce Order Items by Product Category
- PHP Snippet 6: Sort WooCommerce Order Items by Product ID
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
$acomes before$b, the function returns a negative number. - If they’re equal, it returns 0.
- If
$acomes 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();
});









This is very helpful.
Could you suggest a solution for sorting by category & then product alphabetically?
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 );