Custom sorting on the search results page

WooCommerce allows to sort the search results by the following criteria:

Sometimes other plugins or your own business rules may add other sorting positions like e.g. alphabetical. The Pro plugin version doesn’t recognize custom sorting rules. To handle it, you have to create a custom snippet like this:

/**
 * Handles order by alphabetical
 * Work only in PHP 7 and higher
 */
add_filter( 'dgwt/wcas/tnt/sort_products', function ( $products, $order ) {

	if ( $order === 'alphabetical' ) {
		usort( $products, function ( $a, $b ) {
			if ( $a->name != $b->name ) {
				return $a->name <=> $b->name;
			}
		});
	}

	return $products;
}, 10, 2 );

You need to add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.