phpwordpresswoocommerceorderssubtotal

Get Order subtotal in WooCommerce


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).

Can anyone help?

This is the current code:

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

Solution

  • Updated:

    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>
    

    Tested and works