I'm stumped if it's possible. I have scouted to only seeing results to sort by SKU on the front end. not the backend. When you edit an order, I want there to be an SKU column that I can sort by ASC/DESC. Is there a plugin out there that achieves this, or a snippet of code that can add in an additional column? Any help would be greatly appreciated. Below is an image illustrating the SKU I'm talking about and would love for it to be moved/duplicated into its own column
Asked
Active
Viewed 1,693 times
2 Answers
0
add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 );
function filter_order_get_items( $items, $order, $types = 'line_item' ) {
if(count($items) < 2) return $items;
if( $types != 'line_item') return $items;
$item_skus = $sorted_items = array();
// Loop through order line items
foreach( $items as $items_id => $item ){
// Check items type: for versions before Woocommerce 3.3
if($item->is_type('line_item')){
$product = $item->get_product(); //
$item_skus[$product->get_sku()] = $items_id;
}
}
// Check items type: for versions before Woocommerce 3.3 (2)
if( count($item_skus) == 0 ) return $items;
// Sorting in ASC order based on SKUs;
ksort($item_skus); // or use krsort() for DESC order
// Loop through sorted $item_skus array
foreach( $item_skus as $sku => $item_id ){
// Set items in the correct order
$sorted_items[$item_id] = $items[$item_id];
}
return $sorted_items;
}

mujuonly
- 11,370
- 5
- 45
- 75
0
@mujuonly has a great answer. I was using it until I found a conflict with woocommerce subscription renewals. The original orders were fine, but renewals orders were missing their shipping method.
Instead I opted to sort the basket items by sku so that they would be saved in the database that way.
add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_items_sku' );
function sort_cart_items_sku() {
// READ CART ITEMS
$products_in_cart = array();
foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
$products_in_cart[ $key ] = $item['data']->get_sku();
}
// SORT CART ITEMS
natsort( $products_in_cart );
// ASSIGN SORTED ITEMS TO CART
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $product_title ) {
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}
Credit: https://businessbloomer.com/woocommerce-sort-cart-items-alphabetically-az/

Richard Scott
- 11
- 2