Skip to content
Advertisement

Reordering multiple fees differently in Woocommerce cart and checkout pages

When adding multiple fees to the checkout in woocommerce, is it possible to prioritise the order of them? Currently it looks like woocommerce orders them by the fee value, so for example: Fee 1 = £10.00, Fee 2 = £20.00, they would get ordered like so:

  • Subtotal
  • Fee 2 = £20.00
  • Fee 1 = £10.00
  • Total

Is it possible to have a custom order at all? Maybe adding a priority to the action the fee is attached to? I would like Fee 2 to always be the last fee item.

Is this achievable?

Advertisement

Answer

In Woocommerce Fees are handled by the WC_Cart_Fee class and if you look to the source code of get_fees() method, you will see that sorting is made by fee amount in sort_fees_callback() with uasort() php sorting function.

The only way to sort differently the displayed fees in cart and checkout pages is to override some templates via the active child theme (or active theme)

1) Sorting Fees by name: First here is a custom function to sort fees by their label name:

function wc_get_sorted_fees(){
    $fees = WC()->cart->get_fees();
    ksort($fees);
    return $fees;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

2) Display – Overriding the templates cart/cart-totals.php and checkout/review-order.php.

In both templates files you will replace the line:

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>

by

<?php foreach ( wc_get_sorted_fees() as $fee ) : ?>

Related: Sort fees by name on WooCommerce Orders and email notifications

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement