Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 830
Valts Vīze Avatar asked Feb 11 '17 22:02

Valts Vīze


1 Answers

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…

like image 141
LoicTheAztec Avatar answered Sep 20 '22 01:09

LoicTheAztec