【问题标题】:How to get and display BACS account details in Woocommerce如何在 Woocommerce 中获取和显示 BACS 帐户详细信息
【发布时间】:2019-05-08 10:50:39
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce payment-gateway orders


    【解决方案1】:

    Bacs 帐户详细信息存储在 wp_options 表中,作为所有 Wordpress 和 Woocommerce 设置的大部分。

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

    $bacs_accounts_info = get_option( 'woocommerce_bacs_accounts');
    

    通常,这些详细信息默认显示在 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;
    }
    

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


    可能的用法:

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

    get_bacs_account_details_html();
    

    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();
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 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 );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并可以正常工作。

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

    【讨论】:

      【解决方案2】:

      在感谢页面,在您的子functions.php中使用操作或使用代码sn-ps插件

      add_action('woocommerce_thankyou', 'customThankYouFunction');

      并在您的function 中写下您的逻辑

      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'];
      } 
      

      您现在可以处理订单

      参考How to get WooCommerce order details

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-06
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多