如何在WooCommerce购物车及生成的订单中显示总节省金额
在WooCommerce购物车、结账页及订单详情页显示总节省金额
你现有的代码已经实现了结账页的总节省显示,接下来我们通过复用计算逻辑+添加对应钩子的方式,把这个功能扩展到购物车和订单详情页:
1. 重构核心计算函数
先把总节省的计算逻辑抽成独立函数,方便多场景复用:
// 计算总节省金额(通用函数) function calculate_total_saved( $source ) { $discount_total = 0; // 处理购物车/结账场景($source 是 WC_Cart 对象) if ( is_a( $source, 'WC_Cart' ) ) { foreach ( $source->get_cart() as $item ) { $product = $item['data']; if ( $product->is_on_sale() ) { $regular_args = array( 'price' => $product->get_regular_price() ); if ( $source->display_prices_including_tax() ) { $active_price = wc_get_price_including_tax( $product ); $regular_price = wc_get_price_including_tax( $product, $regular_args ); } else { $active_price = wc_get_price_excluding_tax( $product ); $regular_price = wc_get_price_excluding_tax( $product, $regular_args ); } $discount_total += ( $regular_price - $active_price ) * $item['quantity']; } } if ( $source->display_prices_including_tax() ) { $discount_total += $source->get_discount_tax(); } $discount_total += $source->get_discount_total(); } // 处理订单详情场景($source 是 WC_Order 对象) elseif ( is_a( $source, 'WC_Order' ) ) { foreach ( $source->get_items() as $item ) { $product = $item->get_product(); if ( $product && $product->is_on_sale() ) { $regular_price = $product->get_regular_price(); $sale_price = $product->get_sale_price(); // 适配订单的含税/不含税设置 if ( $source->get_prices_include_tax() ) { $regular_price = wc_get_price_including_tax( $product, array( 'price' => $regular_price ) ); $sale_price = wc_get_price_including_tax( $product ); } else { $regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $regular_price ) ); $sale_price = wc_get_price_excluding_tax( $product ); } $discount_total += ( $regular_price - $sale_price ) * $item->get_quantity(); } } // 加上订单的折扣总额 $discount_total += $source->get_discount_total(); if ( $source->get_prices_include_tax() ) { $discount_total += $source->get_discount_tax(); } } return $discount_total; }
2. 购物车页面显示总节省
添加购物车对应的钩子,调用上面的计算函数:
// 购物车页面显示总节省 add_action( 'woocommerce_cart_totals_after_order_total', 'show_total_saved_cart', 1000 ); function show_total_saved_cart() { $discount_total = calculate_total_saved( WC()->cart ); if ( $discount_total > 0 ) { $text = __( "SÄÄSTÖSI", "woocommerce" ); printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">-%s</td></tr>', $text, $text, wc_price($discount_total) ); } }
3. 结账页面保留原有功能(复用重构后的函数)
替换你原来的结账页代码,用重构后的函数:
// 结账页面显示总节省 add_action( 'woocommerce_review_order_after_order_total', 'show_total_saved_checkout', 1000 ); function show_total_saved_checkout() { $discount_total = calculate_total_saved( WC()->cart ); if ( $discount_total > 0 ) { $text = __( "SÄÄSTÖSI", "woocommerce" ); printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">-%s</td></tr>', $text, $text, wc_price($discount_total) ); } }
4. 订单详情页显示总节省
通过钩子添加到订单详情页,无需修改order-details.php模板:
// 订单详情页显示总节省 add_action( 'woocommerce_order_details_after_order_table', 'show_total_saved_order', 10 ); function show_total_saved_order( $order ) { $discount_total = calculate_total_saved( $order ); if ( $discount_total > 0 ) { $text = __( "SÄÄSTÖSI", "woocommerce" ); echo '<div class="total-saved order-total">'; echo '<strong>' . $text . ':</strong> ' . wc_price( $discount_total, array( 'currency' => $order->get_currency() ) ); echo '</div>'; } }
说明
- 重构后的
calculate_total_saved函数支持两种场景:传入购物车对象(WC_Cart)或订单对象(WC_Order),自动适配计算逻辑 - 所有代码可以直接添加到你的主题
functions.php文件,或者自定义插件中 - 样式可以通过
.total-saved类来自定义,和你现有结账页的样式保持一致
内容的提问来源于stack exchange,提问作者Krashkat
相关产品推荐
相关产品推荐

