本文介绍了使用Woocommerce钩子从匹配的订单项元值中显示所选的BACS帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

基于">选择要在其中显示的BACS帐户WooCommerce中的谢谢页面" "针对我的问题之一进行了回答,我更改了Woocommerce核心文件"class-wc-gateway-bacs.php",以便选择在谢谢"页面上显示的正确的银行帐户,该帐户与带有购买产品的变体属性.

Based on "Select BACS account to show in thankyou page in WooCommerce" answer code to one of my questions, I have changed the Woocommerce core file "class-wc-gateway-bacs.php" In order to select the correct bank account displayed on thankyou page, that matches with the variation attribute of a purchased product.

在第255行之后,我已将下一个代码添加到"class-wc-gateway-bacs.php"中:

I have added the next code to "class-wc-gateway-bacs.php" after line 255:

foreach ( $order->get_items() as $item ) {$sede = $item->get_meta("pa_sede");};
if ( $bacs_account->sort_code != $sede ) { continue; };

因此,从bank_details()函数修改的代码段的结果如下:

So the modified code section from bank_details() function, results as follows:

            foreach ( $bacs_accounts as $bacs_account ) {
                $bacs_account = (object) $bacs_account;

                foreach ( $order->get_items() as $item ) {$sede = $item->get_meta("pa_sede");};
                if ( $bacs_account->sort_code != $sede ) { continue; };

                if ( $bacs_account->account_name ) {
                    $account_html .= '<h3 class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</h3>' . PHP_EOL;
                }
                $account_html .= '<ul 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 .= '<li class="' . esc_attr( $field_key ) . '">' . wp_kses_post( $field['label'] ) . ': <strong>' . wp_kses_post( wptexturize( $field['value'] ) ) . '</strong></li>' . PHP_EOL;
                        $has_details   = true;
                    }
                }
                $account_html .= '</ul>';
            }

但是,这不是一个好习惯,我想在其中使用钩子函数,而不要与class-wc-gateway-bacs.php混为一谈.

However, Is not a good practice and I would like to use a hooked function in the instead of messing with class-wc-gateway-bacs.php.

任何帮助将不胜感激.

推荐答案

以下挂钩函数将避免覆盖Woocommerce class-wc-gateway-bacs.php核心文件,使用订单项中的特定变体属性值来显示匹配的银行帐户. Woocommerce订单收到页面(谢谢):

The following hooked function will avoid overwriting Woocommerce class-wc-gateway-bacs.php core file, using a specific variation attribute value from order items to display the matching bank account(s) in Woocommerce Order received page (thankyou):

add_filter( 'woocommerce_bacs_accounts', 'filter_woocommerce_bacs_accounts_callback', 10, 1 );
function filter_woocommerce_bacs_accounts_callback( $bacs_accounts ){
    if ( empty($bacs_accounts) ) {
        return $bacs_accounts; // Exit
    }

    if( is_wc_endpoint_url('order-received') ) {
        $endpoint = 'order-received';
    } elseif( is_wc_endpoint_url('view-order') ) {
        $endpoint = 'view-order';
    } else {
        return $bacs_accounts; // Exit
    }

    // Get the WC_Order Object
    $order = wc_get_order( get_query_var($endpoint) );

    $sort_codes = []; // Initializing variable array

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        $sort_codes[] = $item->get_meta("pa_sede");
    }

    if ( empty($sort_codes) ) {
        return $bacs_accounts; // Exit
    }

    // Loop through Bacs accounts
    foreach ( $bacs_accounts as $key => $bacs_account ) {
        $bacs_account = (object) $bacs_account;

        // Remove the non matching bank accounts
        if ( ! in_array($bacs_account->sort_code, $sort_codes ) ) {
            unset($bacs_accounts[$key]);
        }
    }
    return $bacs_accounts;
}

代码进入您的活动子主题(或活动主题)function.php文件中.经过测试并可以运行(它应与您的产品属性一起用于 pa_sede 的变体).

Code goes in function.php file of your active child theme (or active theme). Tested and work (it should work with your product attribute for variation pa_sede).

这篇关于使用Woocommerce钩子从匹配的订单项元值中显示所选的BACS帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 05:04