调整WooCommerce结账页价格条目排序及相关PHP代码咨询

WooCommerce 合计表格排序与字段获取方案
调整条目顺序PHP实现
直接将以下代码添加到你的主题functions.php文件或自定义功能插件中即可实现要求的排序:
add_filter( 'woocommerce_get_order_item_totals', 'customize_order_total_rows_order', 20, 3 ); function customize_order_total_rows_order( $total_rows, $order, $tax_display ) { // 定义要求的排序规则 $sorted_order = [ 'cart_subtotal', // 对应subtotal小计 'discount', // 对应discount总折扣 'shipping', // 对应shipping运费 'order_total' // 对应total总价 ]; $new_rows = []; // 按自定义顺序重组数组 foreach ( $sorted_order as $row_key ) { if ( isset( $total_rows[ $row_key ] ) ) { $new_rows[ $row_key ] = $total_rows[ $row_key ]; unset( $total_rows[ $row_key ] ); } } // 合并其他可能存在的自定义合计项 return array_merge( $new_rows, $total_rows ); }
单独获取总价、运费的代码
订单场景(传入订单对象$order)
- 获取运费(不含税):
$shipping_amount = $order->get_shipping_total(); - 获取运费(含税):
$shipping_amount_with_tax = $order->get_shipping_total() + $order->get_shipping_tax(); - 获取订单总价:
$total_price = $order->get_total();
购物车/结算页场景
- 获取运费(不含税):
$shipping_amount = WC()->cart->get_shipping_total(); - 获取购物车总价:
$total_price = WC()->cart->get_total();
内容的提问来源于stack exchange,提问作者Wsmay
相关产品推荐
相关产品推荐

