Created
November 30, 2017 12:55
-
-
Save ChromeOrange/e42e681cf3bffefb3bb7e135a7d54154 to your computer and use it in GitHub Desktop.
Add SKU to PDF Invoice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add SKU to PDF order heading | |
*/ | |
add_filter ( 'pdf_template_table_headings' , 'custom_pdf_table_headers' ); | |
function custom_pdf_table_headers() { | |
$headers = '<table class="shop_table orderdetails" width="100%">' . | |
'<thead>' . | |
'<tr><th colspan="7" align="left"><h2>' . esc_html__('Order Details', PDFLANGUAGE) . '</h2></th></tr>' . | |
'<tr>' . | |
'<th width="5%" valign="top" align="right">' . __( 'Qty', PDFLANGUAGE ) . '</th>' . | |
'<th width="10%" valign="top" align="left">' . __( 'SKU', PDFLANGUAGE ) . '</th>' . | |
'<th width="40%" valign="top" align="left">' . __( 'Product', PDFLANGUAGE ) . '</th>' . | |
'<th width="9%" valign="top" align="right">' . __( 'Price Ex', PDFLANGUAGE ) . '</th>' . | |
'<th width="9%" valign="top" align="right">' . __( 'Total Ex.', PDFLANGUAGE ) . '</th>' . | |
'<th width="7%" valign="top" align="right">' . __( 'Tax', PDFLANGUAGE ) . '</th>' . | |
'<th width="10%" valign="top" align="right">' . __( 'Price Inc', PDFLANGUAGE ) . '</th>' . | |
'<th width="10%" valign="top" align="right">' . __( 'Total Inc', PDFLANGUAGE ) . '</th>' . | |
'</tr>' . | |
'</thead>' . | |
'</table>'; | |
return $headers; | |
} | |
/** | |
* Add SKU to order line | |
*/ | |
add_filter ( 'pdf_template_line_output' , 'custom_pdf_lines', 10, 2 ); | |
function custom_pdf_lines ( $line, $order_id ) { | |
global $woocommerce; | |
$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(); | |
$pdflines = '<table width="100%">'; | |
$pdflines .= '<tbody>'; | |
if ( sizeof( $order->get_items() ) > 0 ) { | |
foreach ( $order->get_items() as $item ) { | |
if ( $item['qty'] ) { | |
$line = ''; | |
// $item_loop++; | |
$_product = $order->get_product_from_item( $item ); | |
$item_name = $item['name']; | |
$item_id = $pre_wc_30 ? $item['variation_id'] : $item->get_id(); | |
$meta_display = ''; | |
if ( version_compare( WC_VERSION, '3.0', '<' ) ) { | |
$item_meta = new WC_Order_Item_Meta( $item ); | |
$meta_display = $item_meta->display( true, true ); | |
$meta_display = $meta_display ? ( ' ( ' . $meta_display . ' )' ) : ''; | |
} else { | |
foreach ( $item->get_formatted_meta_data() as $meta_key => $meta ) { | |
$meta_display .= '<br /><small>(' . $meta->display_key . ':' . wp_kses_post( strip_tags( $meta->display_value ) ) . ')</small>'; | |
} | |
} | |
if ( $meta_display ) { | |
$meta_output = apply_filters( 'pdf_invoice_meta_output', $meta_display ); | |
$item_name .= $meta_output; | |
} | |
$line = '<tr>' . | |
'<td valign="top" width="5%" align="right">' . $item['qty'] . ' x</td>' . | |
'<td valign="top" width="10%">' . $_product->get_sku() . '</td>' . | |
'<td valign="top" width="40%">' . stripslashes( $item_name ) . '</td>' . | |
'<td valign="top" width="9%" align="right">' . wc_price( $item['line_subtotal'] / $item['qty'], array( 'currency' => $order_currency ) ) . '</td>' . | |
'<td valign="top" width="9%" align="right">' . wc_price( $item['line_subtotal'], array( 'currency' => $order_currency ) ) . '</td>' . | |
'<td valign="top" width="7%" align="right">' . wc_price( $item['line_subtotal_tax'] / $item['qty'], array( 'currency' => $order_currency ) ). '</td>' . | |
'<td valign="top" width="10%" align="right">' . wc_price( ( $item['line_subtotal'] + $item['line_subtotal_tax'] ) / $item['qty'], array( 'currency' => $order_currency ) ). '</td>' . | |
'<td valign="top" width="10%" align="right">' . wc_price( $item['line_subtotal'] + $item['line_subtotal_tax'], array( 'currency' => $order_currency ) ). '</td>' . | |
'</tr>'; | |
$pdflines .= $line; | |
} | |
} | |
} | |
$pdflines .= '</tbody>'; | |
$pdflines .= '</table>'; | |
return $pdflines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice add on, unfortunately if enabled it breaks the "Adding background colour to the PDF product lines" mod :
` .orderdetails {
border-collapse:collapse;
}
https://gist.github.com/ChromeOrange/3a95922f52cb2c7140d97894217bd264#file-gistfile1-txt