Your WooCommerce shopping cart might look messy when it contains many products. Your specific business, besides, might require you buy “Part 1” first and “Part 2” after.
A way to tidy up the WooCommerce shopping basket is – for example – to sort products based on their title, from A to Z. As usual, this can be done with a few lines of PHP, even if you have no clue about coding… feel free to copy, paste, and enjoy the snippet π
PHP Snippet: Sort Products Alphabetically @ WooCommerce Cart
/**
* @snippet Sort Products Alphabetically @ WooCommerce Cart
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith Woo 3.7
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_cart_loaded_from_session', 'bbloomer_sort_cart_items_alphabetically' );
function bbloomer_sort_cart_items_alphabetically() {
// READ CART ITEMS
$products_in_cart = array();
foreach ( WC()->cart->get_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 ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}
Thank you for your usefull posts. Is there any way to sort products in checkout list according to category? I need this feature to help storekeeper to find the products in the order quickly.
Sarah, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo,
Thanks for this snippet! It worked like a charm!
May I ask for your help to do the same on the order received page?
It seems it uses a different hook and I couldn’t find out how to make this work.
Thanks in advance!
Hi Rafael, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi,
Is it possible to sort the order item name and by line_total?
Hi Rio, thanks so much for your comment! Yes, this is definitely possible, but Iβm afraid itβs custom work. If youβd like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi there, how would i go about if i wanted to order by categories for example a restaurant might want starters to be at the top and mains after so that it is organised. Love to hear back from you.
Regards Sam
Hi Sam, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Is possible to sort the product as [AaBbCc] instead of [ABCabc]?
Hello David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Would you know how to change this to order by product category? I’ve made a few attempts, but I am not very familiar with woocommerce coding.
Thanks to anyone who can help.
Hello Gerry – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Hi!
Last year I was looking for something like this, only shorted by Id or Shipping, it’s easy to change to any other data.
Thank you so much!!
Excellent π
It works!
Yay π
Hi Rodolfo,
Thank you for your code snippet.
It worked like a charm!
Excellent, thank you Nick π