简体   繁体   中英

How to Change Custom Fee order (Ascending/Descending) on cart page (woocommerce )

In woocommerce, I have added a custom fee using the following code:

add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1 );
function custom_fee_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // The percetage
    $percent = 10; // 15%
    // The cart total
    $cart_total = $cart_object->cart_contents_total; 

    // The conditional Calculation
    $fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;

    if ( $fee != 0 ) 
        $cart_object->add_fee( __( "Gratuity", "woocommerce" ), $fee, false );
}

I just want to swipe the fees order like I want "Fee per person" after the sub total and "Gratuity" after "Fee per person".

这是附上的截图

the WooCommerce class WC_Cart_Fees is sorting by default the fees by amount.

reference WC_Cart_Fees

and in order to modify the default behavior of WooCommerce you need to override cart-totals.php

which you can find under under woocommerce plugin dir woocommerce/templates/cart/cart-totals.php

create dir under your child theme name woocommerce/cart and the copy that file to this dir

go to line 61 which you can find the following code :

    <?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
        <tr class="fee">
            <th><?php echo esc_html( $fee->name ); ?></th>
            <td data-title="<?php echo esc_attr( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
        </tr>
    <?php endforeach; ?>

change that code to the following :

<?php
  $array = json_decode(json_encode(WC()->cart->get_fees()), true);
  ksort($array); // 

  foreach ($array as $fee): ?>
        <tr class="fee">
            <th><?php echo esc_html($fee['name']); ?></th>
            <td data-title="<?php echo esc_attr($fee['name']); ?>"><?php echo 
  $fee['total']; ?></td>
        </tr>
    <?php endforeach;?>

code explanation :

basicly what we have done here is geting all fees from the WC class and convert it to array using php built in function json_encode() in order to be able to sort the array in anyway we need, i used ksort() function to sort the array based on the key in ascending order, and then print back the fees:

here is screenshot of the output :

在此处输入图片说明

You can copy the cart-fee.php template as kashalo explained.

But if the only thing you need to change is the fee order, you can override the sort function with the checkout_sort_fees filter.

First example: reverse the order.

add_action( 'woocommerce_sort_fees_callback', 'reverse_sort_fees' );
function reverse_sort_fees( $order )
{
    return -$order;
}

You can use a more sophisticated sort, eg sort by fee name.

add_action( 'woocommerce_sort_fees_callback', 'alpha_sort_fees', 10, 3 );
function alpha_sort_fees( $order, $a, $b )
{
    return $a->name > $b->name ? 1 : -1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM