如何在 Woocommerce 中获取和显示 BACS 帐户详细信息

R.S*_*aby 1 php wordpress payment-gateway orders woocommerce

我有一个非常简单的想法,但我不知道如何在 WooCommerce 中实现。

在我的商店中,我启用了一些付款方式,也通过银行转帐付款。但是当客户选择银行转账时,他会看到进行转账所需的数据。但在那之后,没有选项可以在感谢页面上显示该数据,每个人都在那里寻找。

有没有一种简单的方法可以再次显示该数据?

Loi*_*tec 6

Bacs 帐户详细信息wp_options作为大多数 Wordpress 和 Woocommerce 设置存储在表中。

可以使用(它提供不同银行帐户的多维数组,因为您可以设置多个)访问它们

$bacs_accounts_info = get_option( 'woocommerce_bacs_accounts');
Run Code Online (Sandbox Code Playgroud)

通常,此详细信息默认显示在 woocommerce 谢谢页面和某些客户电子邮件通知中......


为了显示格式化的银行账户信息,我构建了这个自定义函数:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


可能的用途:

1) 在任何模板或 php 代码中,您将只用于显示此帐户详细信息:

get_bacs_account_details_html();
Run Code Online (Sandbox Code Playgroud)

2)作为一个钩子函数(你将在其中设置你想要的动作钩子)。

下面是一个示例用法,它将在我的账户订单视图中显示此银行账户详细信息,用于将 BACS 作为支付网关和“暂停”状态的订单:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。

在此处输入图片说明

3)作为短代码 [bacs_account_details]

add_shortcode( 'bacs_account_details', 'shortcode_bacs_account_details' );
function shortcode_bacs_account_details() {
    get_bacs_account_details_html( false );
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和炒锅。

  • 然后您可以在页面、帖子或自定义帖子的任何 Wordpress 编辑器中使用它: [bacs_account_details]
  • 或者在 PHP 代码中: echo do_shortcode('[bacs_account_details]');