Select BACS account to show in thankyou page in Wo

2019-05-30 11:33发布

I have a WooCommerce store in which one product can be personalized by more than one artist. Every artist have their own bank account to receive their payment; so I need the bank account that appears in the thankyou page, and in the corresponding email, be the one belonging to the chosen artist. To identify each bank account and artist, I did the next:

  1. I assigned a 3 character identifier using the slug for every product variation (artist).
  2. I also assigned the same 3 character identifier using the sort code field for every bank account in the payment gateway.

Now, I need to find which bank account have a sort code equal to the selected variation slug account_details[x]['sort_code'] = (the variation slug)

Can someone point me in the right direction? I need a loop that disables all the rows in account_details except for the one that matches the variation slug.


I found the way to select the bank account by comparing it to a a string. To do so, I added the condition if ( $bacs_account->sort_code != 'ztc' ) { continue; } in line 255 of the file class-wc-gateway-bacs.php

    foreach ( $bacs_accounts as $bacs_account ) {
        $bacs_account = (object) $bacs_account; if ( $bacs_account->sort_code != 'ztc' ) { 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;

However, I cant find the way to get the variation slug to compare it to $bacs_account->sort_code (instead of the string). Also, I think it would be better this to be modified by the functions.php file instead of messing with the class-wc-gateway-bacs.php file.

Can someone can help me in order to do any of this?

2条回答
beautiful°
2楼-- · 2019-05-30 12:05

Done!

Found the way to get the variation slug from https://stackoverflow.com/questions/53009224/get-order-item-meta-data-in-an-unprotected-array-in-woocommerce-3. So I just added:

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

after line 255 in the file "class-wc-gateway-bacs.php". I also commented lines 272-275 to hide the sort code field as it is usefull to select the account, but has no use for the user.

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

Everything is working just as expected.

查看更多
Deceive 欺骗
3楼-- · 2019-05-30 12:22

I worked on a similar situation, but I enabled gateways on the checkout page. Gateways changes after every order in a cyclic fashion. Suppose first order used gateway 1, second uses gateway 2, third gateway 3 again 4 uses gateway 1. I have used CSS to hide / display only one gateway at a time based on gateway used for previous order.

function filter_gateways($gateways){
  global $woocommerce;
  //$WC_Payment_Gateway = wc_get_payment_gateway_by_order( $order );
  $latest_order_id = get_last_order_id(); //Get latest used gateway.
  $order_method = get_post_meta( $latest_order_id, '_payment_method', true );
//Disable /hide gateways based on last order.
    if ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneC') {?>
    <style type="text/css">
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneF{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneK{display: none !important;}
    </style>
    <?php

    } elseif ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneE') {?>
    <style type="text/css">
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneE{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneK{display: none !important;}
    </style>

    <?php


    } elseif ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneF') {?>
    <style type="text/css">
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneE{display: none !important;}
    li.payment_method_nmi_gateway_woocommerce_credit_card_cloneF{display: none !important;}
    </style>



    return $gateways;
}

add_filter('woocommerce_available_payment_gateways','filter_gateways');

So in your case, get the slug url and based on that there should be some styling convention followed in BACS displayed from using which you can disable other gateways.

To get slug in case you have not: 1.First get the product id from order id. 2. Then you can get the slug using product id (Woocommerce: How do I get the product slug from the id? )

Hope that helps.

查看更多
登录 后发表回答