Sort cart fees alphabetically in WooCommerce

/**
 * Sorts cart fees alphabetically.
 *
 * @see https://wpcodebook.com/woocommerce-sort-cart-fees-alphabetically/
 * @see https://github.com/woocommerce/woocommerce/blob/8.0.2/plugins/woocommerce/includes/class-wc-cart-fees.php#L147
 *
 * @param int $res Sort order, -1 or 1.
 * @param stdClass $a Fee object.
 * @param stdClass $b Fee object.
 */
add_filter( 'woocommerce_sort_fees_callback', function ( $res, $a, $b ) {
	return ( $a->name > $b->name ? 1 : -1 );
}, 10, 3 );

The snippet will sort fees by title, ascending. To sort by title, descending:

return ( $a->name > $b->name ? -1 : 1 );

By default, in WooCommerce, fees are sorted by amount, descending.

Leave a Comment