mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-04 12:22:24 +08:00
https://stackoverflow.com/questions/77532361/sort-order-items-alphabetically-with-some-exceptions
21 lines
761 B
Text
21 lines
761 B
Text
add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
|
|
|
|
$prioritizedItems = ['PO#', 'Terms', 'Discount']; // Items which needs to be displayed first
|
|
|
|
uasort($items, function($a, $b) use ($prioritizedItems) {
|
|
// Check if either item is in the prioritized list
|
|
$a_priority = in_array($a['name'], $prioritizedItems);
|
|
$b_priority = in_array($b['name'], $prioritizedItems);
|
|
|
|
// If both or neither of them are prioritized, then you can sort alphabetically
|
|
if ($a_priority === $b_priority) {
|
|
return strnatcmp($a['name'], $b['name']);
|
|
}
|
|
|
|
// If a single one is prioritized, it should come first
|
|
return $a_priority ? -1 : 1;
|
|
});
|
|
|
|
return $items;
|
|
|
|
}, 10, 2 );
|