【问题标题】:Sort fees by name on WooCommerce Orders and email notifications在 WooCommerce 订单和电子邮件通知中按名称对费用进行排序
【发布时间】:2020-12-06 12:14:28
【问题描述】:

我需要在 WooCommerce 订单中按名称而不是按价格对费用进行排序。

使用 Reordering multiple fees differently in Woocommerce cart and checkout pages 答案代码,我已设法在购物车和结帐页面中按名称对费用进行排序。但它不适用于订单确认和发送的电子邮件。

感谢您的帮助。

【问题讨论】:

  • @ShaunHearnden 看看下面……

标签: php wordpress woocommerce orders fee


【解决方案1】:

更新

要按客户订单和电子邮件通知中的名称对费用进行排序,您将使用以下内容:

// Custom function that sort displayed fees by name on order items
function wc_get_sorted_order_item_totals_fee_rows( &$total_rows, $tax_display, $order ) {
    $fees = $order->get_fees();

    if ( $fees ) {
        $fee_names = []; // initializing

        // First Loop
        foreach ( $fees as $fee_id => $fee ) {
            $fee_names[$fee_id] = $fee->get_name();
        }
        asort($fee_names); // Sorting by name

        // 2nd Loop
        foreach ( $fee_names as $fee_id => $fee_name ) {
            $fee = $fees[$fee_id];

            if ( apply_filters( 'woocommerce_get_order_item_totals_excl_free_fees', empty( $fee['line_total'] ) && empty( $fee['line_tax'] ), $fee_id ) ) {
                continue;
            }

            $total_rows[ 'fee_' . $fee->get_id() ] = array(
                'label' => $fee->get_name() . ':',
                'value' => wc_price( 'excl' === $tax_display ? $fee->get_total() : $fee->get_total() + $fee->get_total_tax(), array( 'currency' => $order->get_currency() ) ),
            );
        }
    }
}

// Display sorted fees by name everywhere on orders and emails
add_filter( 'woocommerce_get_order_item_totals', 'display_date_custom_field_value_on_order_item_totals', 10, 3 );
function display_date_custom_field_value_on_order_item_totals( $total_rows, $order, $tax_display ){
    // Initializing variables
    $new_total_rows = $item_fees = [];

    // 1st Loop - Check for fees
    foreach ( $total_rows as $key => $values ) {
        if( strpos($key, 'fee_') !== false ) {
            $item_fees[] = $key;
        }
    }

    if( count($item_fees) > 1 ) {
        // 2nd Loop - Remove item fees total lines
        foreach ( $item_fees as $key ) {
            unset($total_rows[$key]);
        }

        $key_start = isset($total_rows['shipping']) ? 'shipping' : ( isset($total_rows['discount']) ? 'discount' : 'cart_subtotal' );

        // 3rd Loop - loop through order total rows
        foreach( $total_rows as $key => $values ) {
            $new_total_rows[$key] = $values;

            // Re-inserting sorted fees
            if( $key === $key_start ) {
                wc_get_sorted_order_item_totals_fee_rows( $new_total_rows, $tax_display, $order );
            }
        }
        return $new_total_rows;
    }
    return $total_rows;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 感谢您,但它仍然无法正常工作。我将在哪里编辑它以首先将订单从最高价格更改为最低价格,反之亦然?感谢您的帮助
  • 抱歉,但它在 Storefront 主题下的上一个 woocommerce 版本上完美运行……我记得您在问题中要求按名称对费用进行排序,那么为什么现在要按价格对它们进行排序?
  • 我会尝试不同的主题。这是因为客户想要两种选择,按名称排序和按价格排序(最低优先)。对不起,我应该在我的问题中提到它。
  • @HarryFox 根本不可能,因为它不是 SQL 查询……所以您可以按名称对它们进行排序,或者按成本进行排序,但两者都不是。
  • 好的,你能告诉我如何先按成本最低吗?我认为这对客户来说是一个更好的选择。感谢您为此提供的所有帮助。
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 2019-01-12
  • 1970-01-01
相关资源
最近更新 更多