在不覆盖核心文件的情况下向BACS帐户字段添加自定义字段 [英] Adding a custom field to BACS account fields without overriding core files

查看:53
本文介绍了在不覆盖核心文件的情况下向BACS帐户字段添加自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况-我对woocommerce电子邮件模板之一进行了更改,但是我确定-这些更改在下一次woocommerce更新后将丢失.

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.

这是更改前的代码:

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?

谢谢

推荐答案

从不覆盖核心文件,并始终使用WooCommerce附带的挂钩进行代码自定义.

如果您找不到通过自定义挂钩函数进行更改的方法(如您在提供的代码中看到的那样),则可以使用 woocommerce_bacs_account_fields 添加自定义代码,而不会覆盖任何WooCommerce核心文件.

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.

因此,用于在BACS帐户字段中添加新字段的代码将是:

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

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试并且可以正常工作……

This code is tested and works…

这篇关于在不覆盖核心文件的情况下向BACS帐户字段添加自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆