WooCommerce结账页面小计与总计显示双货币问题排查
Hey there, let's tackle this problem together. The issue with using wc_price is that it affects all price displays across your site, but you only need the dual currency (ETH + USD) on the subtotal and total lines in checkout. Instead, we should target the specific order total rows with a more precise hook.
Solution Code
First, let's adjust your approach to focus only on the checkout totals section:
// Reusable function to convert ETH price to USD function convert_eth_to_usd( $eth_price ) { $conversion_rate = 780.730022; // Update this rate regularly for accuracy $usd_price = $eth_price * $conversion_rate; return number_format( $usd_price, 2, '.', ',' ); } // Modify checkout subtotal and total to show dual currencies add_filter( 'woocommerce_get_order_item_totals', 'display_dual_currency_on_checkout_totals', 10, 3 ); function display_dual_currency_on_checkout_totals( $total_rows, $order, $tax_display ) { // Only run this on the checkout page (not order receipts or admin) if ( is_checkout() && ! is_wc_endpoint_url() ) { // Update Subtotal line if ( isset( $total_rows['cart_subtotal'] ) ) { $eth_subtotal = $order->get_subtotal(); $usd_subtotal = convert_eth_to_usd( $eth_subtotal ); $total_rows['cart_subtotal']['value'] = "ETH " . number_format( $eth_subtotal, 2 ) . " ($ " . $usd_subtotal . ")"; } // Update Total line if ( isset( $total_rows['order_total'] ) ) { $eth_total = $order->get_total(); $usd_total = convert_eth_to_usd( $eth_total ); $total_rows['order_total']['value'] = "ETH " . number_format( $eth_total, 2 ) . " ($ " . $usd_total . ")"; } } return $total_rows; }
Key Details
- Targeted Hook:
woocommerce_get_order_item_totalsis designed specifically for modifying the rows in the checkout totals table, so it won't mess up price displays elsewhere on your site. - Reusable Conversion: We kept your conversion logic in a dedicated function for easy updates (you could even replace the static rate with an API call later to get real-time prices).
- Checkout-Only Execution: The
is_checkout()check ensures this only runs on the checkout page, not on order confirmation pages or in the admin dashboard.
Quick Troubleshooting
- If your theme uses custom keys for totals (instead of
cart_subtotalororder_total), addvar_dump($total_rows);inside the function temporarily to see the exact keys your theme uses. - If taxes are applied and you want dual currencies there too, you can add similar logic for rows like
taxorshipping_tax.
内容的提问来源于stack exchange,提问作者Roger van Eijk
相关产品推荐
相关产品推荐

