R.Slaby
R.Slaby

Reputation: 401

How to get and display BACS account details in Woocommerce

I have a very simple idea, but I do not know how to do it in WooCommerce.

In my store I have enabled a few payment options, also paying via bank transfer. But when client chose bank transfer he sees data needed to make a transfer. But after that, there is no option to display that data on thank you page, where everybody looking for that.

There is an easy way to show that data again?

Upvotes: 3

Views: 11864

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254212

The Bacs account details are stored in wp_options table as most of all Wordpress and Woocommerce settings.

They can be accessed using (which gives a multi-dimensional array of different bank accounts, as you can set many):

$bacs_accounts_info = get_option( 'woocommerce_bacs_accounts');

Normally this details are displayed by default on woocommerce thankyou page and in some customer email notifications…


To display the formatted bank accounts information, I have built this custom function:

// Utility function, to display BACS accounts details
function get_bacs_account_details_html( $echo = true, $type = 'list' ) {

    ob_start();

    $gateway    = new WC_Gateway_BACS();
    $country    = WC()->countries->get_base_country();
    $locale     = $gateway->get_country_locale();
    $bacs_info  = get_option( 'woocommerce_bacs_accounts');

    // Get sortcode label in the $locale array and use appropriate one
    $sort_code_label = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' );

    if( $type == 'list' ) :
    ?>
    <div class="woocommerce-bacs-bank-details">
    <h2 class="wc-bacs-bank-details-heading"><?php _e('Our bank details'); ?></h2>
    <?php
    $i = -1;
    if ( $bacs_info ) : foreach ( $bacs_info as $account ) :
    $i++;

    $account_name   = esc_attr( wp_unslash( $account['account_name'] ) );
    $bank_name      = esc_attr( wp_unslash( $account['bank_name'] ) );
    $account_number = esc_attr( $account['account_number'] );
    $sort_code      = esc_attr( $account['sort_code'] );
    $iban_code      = esc_attr( $account['iban'] );
    $bic_code       = esc_attr( $account['bic'] );
    ?>
    <h3 class="wc-bacs-bank-details-account-name"><?php echo $account_name; ?>:</h3>
    <ul class="wc-bacs-bank-details order_details bacs_details">
        <li class="bank_name"><?php _e('Bank'); ?>: <strong><?php echo $bank_name; ?></strong></li>
        <li class="account_number"><?php _e('Account number'); ?>: <strong><?php echo $account_number; ?></strong></li>
        <li class="sort_code"><?php echo $sort_code_label; ?>: <strong><?php echo $sort_code; ?></strong></li>
        <li class="iban"><?php _e('IBAN'); ?>: <strong><?php echo $iban_code; ?></strong></li>
        <li class="bic"><?php _e('BIC'); ?>: <strong><?php echo $bic_code; ?></strong></li>
    </ul>
    <?php endforeach; endif; ?>
    </div>
    <?php
    else :
    ?>
    <h2><?php _e( 'Account details', 'woocommerce' ); ?>:</h2>
    <table class="widefat wc_input_table" cellspacing="0">
        <thead>
            <tr>
                <th><?php _e( 'Account name', 'woocommerce' ); ?></th>
                <th><?php _e( 'Account number', 'woocommerce' ); ?></th>
                <th><?php _e( 'Bank name', 'woocommerce' ); ?></th>
                <th><?php echo $sort_code_label; ?></th>
                <th><?php _e( 'IBAN', 'woocommerce' ); ?></th>
                <th><?php _e( 'BIC / Swift', 'woocommerce' ); ?></th>
            </tr>
        </thead>
        <tbody class="accounts">
            <?php
            $i = -1;
            if ( $bacs_info ) {
                foreach ( $bacs_info as $account ) {
                    $i++;

                    echo '<tr class="account">
                        <td>' . esc_attr( wp_unslash( $account['account_name'] ) ) . '</td>
                        <td>' . esc_attr( $account['account_number'] ) . '</td>
                        <td>' . esc_attr( wp_unslash( $account['bank_name'] ) ) . '</td>
                        <td>' . esc_attr( $account['sort_code'] ) . '</td>
                        <td>' . esc_attr( $account['iban'] ) . '</td>
                        <td>' . esc_attr( $account['bic'] ) . '</td>
                    </tr>';
                }
            }
            ?>
        </tbody>
    </table>
    <?php
    endif;
    $output = ob_get_clean();

    if ( $echo )
        echo $output;
    else
        return $output;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.


POSSIBLE USAGES:

1) In any template or php code you will just use to display this account details:

get_bacs_account_details_html();

2) As a hooked function (where you will set your desired action hook).

Here is an example usage that will display this bank account details in My account order view, for orders that have BACS as payment gateway and an "on hold" status:

add_action( 'woocommerce_view_order', 'display_bacs_account_details_on_view_order', 5, 1 );
function display_bacs_account_details_on_view_order( $order_id ){
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    if( $order->get_payment_method() === 'bacs' && $order->get_status() === 'on-hold' ){
        get_bacs_account_details_html();
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

  1. As a shortcode [bacs_account_details]:

    add_shortcode( 'bacs_account_details', 'shortcode_bacs_account_details' ); function shortcode_bacs_account_details() { get_bacs_account_details_html( false ); }

Code goes in function.php file of your active child theme (or active theme). Tested and wok.

  • Then you can use it in any Wordpress editor of a page, a post or a custom post: [bacs_account_details]
  • Or in the PHP code: echo do_shortcode('[bacs_account_details]');

Upvotes: 11

Mahmoud Gamal
Mahmoud Gamal

Reputation: 330

Use action on thank you page, In your child functions.php or using code snippets plugin

add_action('woocommerce_thankyou', 'customThankYouFunction');

and in your function write your logic

function customThankYouFunction ($order_id) {
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data(); // The Order data
    $order_id = $order_data['id'];
    $order_parent_id = $order_data['parent_id'];
    $order_status = $order_data['status'];
    $order_currency = $order_data['currency'];
    $order_version = $order_data['version'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method_title = $order_data['payment_method_title'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method = $order_data['payment_method'];
} 

You can deal with order now

Reference How to get WooCommerce order details

Upvotes: 1

Related Questions