Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PierreNodles/ce305eafb049b4e9e9a91bfdf4db2334 to your computer and use it in GitHub Desktop.
Save PierreNodles/ce305eafb049b4e9e9a91bfdf4db2334 to your computer and use it in GitHub Desktop.
Show VAT Details & Discount - Woocommerce PDF Invoice
add_filter( 'pdf_template_order_totals' , 'coupon_pdf_template_order_totals', 10, 2 );
function coupon_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();
unset( $order_item_totals['payment_method'] );
$output = '';
// Making sure that the VAT details are only included once in the foreach loop
// With a "cleaned" var
$cleaned = false;
foreach ( $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 (!$cleaned) {
/*** ADDING VAT DETAILS ON TOTALS ****/
if ( $order->get_total_tax()>0 ) {
$tax_items = $order->get_tax_totals();
if ( count( $tax_items ) > 1 ) {
foreach ( $tax_items as $tax_item ) {
$tax_item_amount = $pre_wc_30 ? woocommerce_price( $tax_item->amount ) : wc_price( $tax_item->amount );
$output .= '<tr>' .
'<td align="right">' . esc_html( $tax_item->label ) . '</td>' .
'<td align="right">' . $tax_item_amount . '</td>' .
'</tr>' ;
}
$total_tax = $pre_wc_30 ? woocommerce_price( $order->get_total_tax() ) : wc_price( $order->get_total_tax() );
$output .= '<tr>' .
'<td align="right">' . __('Total Tax', 'woocommerce-pdf-invoice') . '</td>' .
'<td align="right">' . $total_tax . '</td>' .
'</tr>' ;
} else {
foreach ( $tax_items as $tax_item ) {
$tax_item_amount = $pre_wc_30 ? woocommerce_price( $tax_item->amount ) : wc_price( $tax_item->amount );
$output .= '<tr>' .
'<td align="right">' . esc_html( $tax_item->label ) . '</td>' .
'<td align="right">' . $tax_item_amount . '</td>' .
'</tr>' ;
}
}
}
/**** Closing the execution in the foreach loop by setting cleaned as true ****/
$cleaned = true;
}
}
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