I am looking for a php snippet that sorts the woocommerce products according to sku in the mini cart/shopping cart, checkout page and thank you page.
The snippet below works well in the store but not for the rest.
Thank you in advance.
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '_sku';
$args['orderby'] = '_sku';
$args['order'] = 'asc';
return $args;
}
2
Answers
Based on Sort WooCommerce cart items by vendor when using Dokan plugin, here is the way to sort cart items by SKU (works for cart items, mini-cart items and order items):
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Assuming Loic’s advice is solid (and I have no reason to doubt any of his contributions), this looks like a simple job for
array_multisort()
.Mocked-up demo: https://3v4l.org/YE75c