3

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>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Zed0121
  • 157
  • 2
  • 3
  • 8

1 Answers1

6

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks works great. is it possible to include discounts in it too (if discounts have been applied) so it reads... Subtotal : £100 and the the discount: -£20; and then finally the Total: £80 – Zed0121 Dec 13 '17 at 14:59
  • The subtotal was displayed as £0 when i tested it, when it should ahve been £6.40. I am using woocommerce 3.2.4 and latest Wordpress version. – Zed0121 Dec 13 '17 at 15:05
  • 1
    I changed it to the following and it worked! // Get order subtotal $order_subtotal = $order->get_subtotal( ); Only thing is my number is missing the final 0. E.g. if the price is £25.10 it is showing as £25.1 If the price is £33.33 it is showing correctly. Only when the currency ends with a 0, it is dropped off. – Zed0121 Dec 13 '17 at 15:24
  • 1
    that worked perfect! Many thanks. If you are able to include the code to display the total discounts that would be even better! – Zed0121 Dec 13 '17 at 16:44
  • 1
    @Zed0121 Updated and done… try it … the `WC_Abstract_Order` method is [`get_discount_total()`](https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_get_discount_total) – LoicTheAztec Dec 13 '17 at 16:53