Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active July 31, 2017 08:46
Show Gist options
  • Save ChromeOrange/7798c46a3bfbc1ce7cddf66d4680fa5b to your computer and use it in GitHub Desktop.
Save ChromeOrange/7798c46a3bfbc1ce7cddf66d4680fa5b to your computer and use it in GitHub Desktop.
Reorder the totals section for WooCommerce PDF Invoice. Changes the SubTotal to include the shipping cost
/**
* Reorder the totals output
*
* Shipping
* Subtotal
* Taxes
* Total
*
* Changes the SubTotal to include the shipping cost
*/
add_filter( 'pdf_template_order_totals', 'custom_totals_layout_pdf_template_order_totals', 10, 2 );
function custom_totals_layout_pdf_template_order_totals( $output, $order_id ){
global $woocommerce;
if (!$order_id) return;
$order = new WC_Order( $order_id );
// Check WC version - changes for WC 3.0.0
$pre_wc_30 = version_compare( WC_VERSION, '3.0', '<' );
$order_currency = $pre_wc_30 ? $order->get_order_currency() : $order->get_currency();
$order_item_totals = $order->get_order_item_totals();
// Clean up shipping value
// Use this line if you just want the shipping value, without the description
// $order_item_totals['shipping']['value'] = wc_price( round( $order->get_shipping_total(), wc_get_price_decimals() ), array( 'currency' => $order_currency ) );
// Grab the basics
$cart_subtotal = $order_item_totals['cart_subtotal'];
$shipping = $order_item_totals['shipping'];
$order_total = $order_item_totals['order_total'];
// Modify the subtotal to include shipping
$subtotal = $order->get_subtotal();
$shipping_cost = $order->get_shipping_total();
$new_subtotal = wc_price( round( ($subtotal + $shipping_cost), wc_get_price_decimals() ), array( 'currency' => $order_currency ) );
$replace_subtotal = wc_price( $subtotal, array( 'currency' => $order_currency ) );
// Update the subtotal array
$cart_subtotal = str_replace( $replace_subtotal, $new_subtotal, $cart_subtotal );
// Remove everything except taxes
unset( $order_item_totals['cart_subtotal'] );
unset( $order_item_totals['shipping'] );
unset( $order_item_totals['payment_method'] );
unset( $order_item_totals['order_total'] );
// Start building a new array in the right order
$new_order_item_totals['shipping'] = $shipping;
$new_order_item_totals['cart_subtotal'] = $cart_subtotal;
// Taxes
foreach ( $order_item_totals as $key => $value ) {
$new_order_item_totals[$key] = $value;
}
$new_order_item_totals['order_total'] = $order_total;
$output = '';
foreach ( $new_order_item_totals as $order_item_total ) {
$output .= '<tr>' .
'<td align="right">' .
'<strong>' . $order_item_total['label'] . '</strong></td>' .
'<td align="right"><strong>' . $order_item_total['value'] . '</strong></td>' .
'</tr>' ;
}
if( $order->get_total_refunded() > 0 ) {
$output .= '<tr>' .
'<td align="right">' .
'<strong>Amount Refunded:</strong></td>' .
'<td align="right"><strong>' . wc_price( $order->get_total_refunded(), array( 'currency' => $order_currency ) ) . '</strong></td>' .
'</tr>' ;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment