WooCommerce shopping cart might look messy when it contains many products. A way to well kept the WooCommerce shopping basket. For this, To sort products based on their title, from A to Z. Let’s have a look at the following piece of code which shows sort products title from A to Z.
Add this code in your theme’s functions.php file of your active child theme (or theme).

add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_items_alphabetically' );
function sort_cart_items_alphabetically() {
global $woocommerce;
// Read Cart Items
$products_in_cart = array();
foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
$products_in_cart[ $key ] = $item['data']->get_title();
}
// 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 ] = $woocommerce->cart->cart_contents[ $cart_key ];
}
$woocommerce->cart->cart_contents = $cart_contents;
}