Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active July 31, 2018 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChromeOrange/6533679 to your computer and use it in GitHub Desktop.
Save ChromeOrange/6533679 to your computer and use it in GitHub Desktop.
Add Bank Details to footer from WooCommerce BACS settings to WooCommerce PDF Invoice
/**
* Add Bank Details to footer from WooCommerce BACS settings
*
* Uses template tag [[PDFBANKDETAILS]]
*
* 1 - Add the template tag to template.php
* 2 - Add this code to the theme functions.php
* 3 - check invoice layout and modify CSS in template.php as necessary
*/
add_filter ( 'pdf_content_additional_content', 'pdf_additional_content_bank_details_footer', 10, 2 );
function pdf_additional_content_bank_details_footer( $content, $order_id ) {
// Get order and store in $order.
$order = wc_get_order( $order_id );
$has_details = false;
// Get sortcode label in the $locale array and use appropriate one.
$sortcode = __( 'Sort code', 'woocommerce' );
$bacs_accounts = get_option( 'woocommerce_bacs_accounts' );
if ( ! empty( $bacs_accounts ) ) {
$account_html = '<table>';
foreach ( $bacs_accounts as $bacs_account ) {
$bacs_account = (object) $bacs_account;
if ( $bacs_account->account_name ) {
$account_html .= '<tr><td class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</td>' . PHP_EOL;
}
// $account_html .= '<tr class="wc-bacs-bank-details order_details bacs_details">' . PHP_EOL;
// BACS account fields shown on the thanks page and in emails.
$account_fields = apply_filters(
'woocommerce_bacs_account_fields', array(
'bank_name' => array(
'label' => __( 'Bank', 'woocommerce' ),
'value' => $bacs_account->bank_name,
),
'account_number' => array(
'label' => __( 'Account number', 'woocommerce' ),
'value' => $bacs_account->account_number,
),
'sort_code' => array(
'label' => $sortcode,
'value' => $bacs_account->sort_code,
),
'iban' => array(
'label' => __( 'IBAN', 'woocommerce' ),
'value' => $bacs_account->iban,
),
'bic' => array(
'label' => __( 'BIC', 'woocommerce' ),
'value' => $bacs_account->bic,
),
), $order_id
);
foreach ( $account_fields as $field_key => $field ) {
if ( ! empty( $field['value'] ) ) {
$account_html .= '<td class="' . esc_attr( $field_key ) . '">' . wp_kses_post( $field['label'] ) . ': <strong><br />' . wp_kses_post( wptexturize( $field['value'] ) ) . '</strong></td>' . PHP_EOL;
$has_details = true;
}
}
$account_html .= '</tr>';
}
$account_html .= '</table>';
}
if ( $has_details ) {
$pdfbankdetails = '<section class="woocommerce-bacs-bank-details">' . esc_html__( 'Our bank details', 'woocommerce' ) . '' . wp_kses_post( PHP_EOL . $account_html ) . '</section>';
$content = str_replace( '[[PDFBANKDETAILS]]', $pdfbankdetails, $content );
} else {
$content = str_replace( '[[PDFBANKDETAILS]]', '', $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment