【问题标题】:Get Order subtotal in WooCommerce在 WooCommerce 中获取订单小计
【发布时间】:2018-05-26 10:40:54
【问题描述】:

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

谁能帮忙?

这是当前代码:

    $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>

【问题讨论】:

    标签: php wordpress woocommerce orders price


    【解决方案1】:

    更新:

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

    // 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>
    

    经过测试和工作

    【讨论】:

    • 谢谢,效果很好。是否也可以在其中包含折扣(如果已应用折扣)所以它显示... 小计:100 英镑,折扣:-20 英镑;最后是总计:80 英镑
    • 小计在我测试时显示为 0 英镑,而应该是 6.40 英镑。我正在使用 woocommerce 3.2.4 和最新的 Wordpress 版本。
    • 我将其更改为以下内容,并且有效! // 获取订单小计 $order_subtotal = $order->get_subtotal( );唯一的问题是我的号码缺少最后的 0。例如如果价格为 25.10 英镑,则显示为 25.1 英镑 如果价格为 33.33 英镑,则显示正确。只有当货币以 0 结尾时,才会被丢弃。
    • 效果很好!非常感谢。如果您能够包含代码以显示总折扣,那就更好了!
    • @Zed0121 更新完成……试试看……WC_Abstract_Order 方法是get_discount_total()
    猜你喜欢
    • 2016-11-01
    • 2018-03-16
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2022-01-14
    相关资源
    最近更新 更多