Adding a custom field to BACS account fields without overriding core files

℡╲_俬逩灬. 提交于 2021-02-19 01:32:33

问题


I have this situation - I made a changes in one of the woocommerce email templates, but I`m sure - these changes will be lost after next woocommerce update.

As I know, I should use theme functions to bypass this problem.

This is the code before changes:

echo '<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(
                    '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'] ) ) {
                        echo '<li class="' . esc_attr( $field_key ) . '">' . esc_attr( $field['label'] ) . ': <strong>' . wptexturize( $field['value'] ) . '</strong></li>' . PHP_EOL;
                    }
                }

                echo '</ul>';

Here is the custom account field code that I want to insert:

'merkis' => array(
    'label' => $merkis,
    'value' => $pasutijums
)

How can I insert my custom code without overriding that core file?

Thanks


回答1:


Never Override core files and always use the WooCommerce included hooks to make code customizations.

If you haven't find the way to make this change through a custom hooked function, as you will see in your provided code, you can use woocommerce_bacs_account_fields filter hook to add your custom code, without overriding any WooCommerce core files.

So the code for adding a new field in BACS account fields, is going to be:

add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
    $account_fields['merkis' ] = array(
        'label' => $merkis,
         'value' => $pasutijums
    );
    return $account_fields;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works…



来源:https://stackoverflow.com/questions/42181880/adding-a-custom-field-to-bacs-account-fields-without-overriding-core-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!