在 WooCommerce 中获取订单小计 [英] Get Order subtotal in WooCommerce

查看:70
本文介绍了在 WooCommerce 中获取订单小计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插件可以输出我的订单的 PDF.其中一个部分显示订单总数.目前这会显示总计,但我希望它显示小计(即应用任何优惠券等之前的订单金额).

I have a plugin that outputs a PDF of my orders. One of the sections displays the order total. Currently this displays the total, but I want it to display the subtotal (i.e. the order amount before any coupons etc are applied).

有人可以帮忙吗?

这是当前代码:

    $order_total = is_callable(array($order, 'get_total')) ? $order->get_total() : $order->order_total; ?>

    <p id="order-total">
<b><?php _e('ORDER TOTAL:', 'woocommerce');?></b>
<span id="order-total-price">£<?php echo $order_total;?></span>

推荐答案

更新:

您可以使用 WC_Abstract_Order 方法 get_subtotal_to_display() 来获取和显示订单小计(但由于它是格式化的价格,我们需要对其进行清理):

You can use the WC_Abstract_Order method get_subtotal_to_display() to get and display the Order subtotal (but as it's a formatted price we need to clean it):

// Get the currency symbol
$currency_symbol = get_woocommerce_currency_symbol( get_woocommerce_currency() );

// Get order total
$order_total = is_callable(array($order, 'get_total')) ? $order->get_total() : $order->order_total;

// Get order subtotal
$order_subtotal = $order->get_subtotal();
// Get the correct number format (2 decimals)
$order_subtotal = number_format( $order_subtotal, 2 );

// Get order total discount
$order_discount_total = $order->get_discount_total();
// Get the correct number format (2 decimals)
$order_discount_total = number_format( $order_discount_total, 2 );

?>
<p id="order-subtotal">
    <b><?php _e('ORDER SUBTOTAL:', 'woocommerce');?></b>
    <span id="order-subtotal-price"><?php echo $currency_symbol . $order_subtotal;?></span>
</p>
<p id="order-total-discount">
    <b><?php _e('ORDER DISCOUNT TOTAL:', 'woocommerce');?></b>
    <span id="order-total-discount-price"><?php echo $currency_symbol . $order_discount_total;?></span>
</p>
<p id="order-total">
    <b><?php _e('ORDER TOTAL:', 'woocommerce');?></b>
    <span id="order-total-price"><?php echo $currency_symbol . $order_total;?></span>
</p>

经过测试并有效

这篇关于在 WooCommerce 中获取订单小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆