Adding a custom field to the BACS account fields without overriding the kernel files

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

As I know, I have to use theme functions to get around this problem.

This is the code before changing:

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 code for the user account field that I want to insert:

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

How can I insert my own code without overriding this main file?

thanks

+5
source share
1 answer

Never override core files and always use WooCommerce hooks to create code settings.

If you have not found a way to make this change using a custom function, as you will see in your provided code, you can use the woocommerce_bacs_account_fields filter to add your code without overriding the main WooCommerce files.

Thus, the code to add a new field to the fields of the BACS account will 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; } 

The code goes in the function.php file of your active child theme (or theme), as well as in any plug-in file.

This code has been verified and works ...

+6
source

Source: https://habr.com/ru/post/1264112/


All Articles