在 Woocommerce 购物车和结帐页面中以不同方式重新排序多项费用

Reordering multiple fees differently in Woocommerce cart and checkout pages

在woocommerce结账中添加多项费用时,是否可以优先考虑它们的顺序?目前看起来 woocommerce 按费用值订购它们,例如:费用 1 = £10.00,费用 2 = £20.00,它们将像这样订购:

是否可以定制订单?也许为费用所附的行动增加优先权?我希望 Fee 2 永远是最后一个收费项目。

这是可以实现的吗?

在 Woocommerce 中,费用由 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()uasort() php 排序功能处理。

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) 按名称对费用进行排序: 首先这是一个自定义函数,用于按标签名称对费用进行排序:

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

2) 显示 - 覆盖模板 cart/cart-totals.phpcheckout/review-order.php.

在两个模板文件中,您将替换行:

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

来自

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

相关: