In WooCommerce, I have a product attribute pa_location
for "location" with which I sort my order items. When order items have the same location, I'd like to sort them by SKU. So, sort first by location and then by SKU.
I looked to "PHP usort sorting multiple fields" but I am unable to figure out how it applies and would work for my function.
I have this custom function that works to sort by pa_location
, but cannot get it to also sort by _sku
.
I tried implementing some of the examples in the referenced post about sorting.
add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
uasort( $items,
function( $a, $b ) {
return strnatcmp( $a['pa_location'], $b['pa_location'] );
}
);
return $items;
}, 10, 2 );
I hope to sort by location then by SKU using this function. Right now it is only sorting by location.