php - 使用 Woocommerce 挂钩从匹配的订单项元值中显示选定的 BACS 帐户
问题描述
基于“选择 BACS 帐户以显示在 WooCommerce 的谢谢页面中”回答我的一个问题的代码,我更改了 Woocommerce 核心文件“class-wc-gateway-bacs.php”为了选择正确的银行帐户显示在谢谢页面,与所购买产品的变体属性相匹配。
我在第 255 行之后将下一个代码添加到“class-wc-gateway-bacs.php”:
foreach ( $order->get_items() as $item ) {$sede = $item->get_meta("pa_sede");};
if ( $bacs_account->sort_code != $sede ) { continue; };
所以从bank_details()
函数修改代码部分,结果如下:
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>';
}
但是,这不是一个好习惯,我想在而不是弄乱 class-wc-gateway-bacs.php 中使用挂钩函数。
任何帮助将不胜感激。
解决方案
以下挂钩函数将避免覆盖 Woocommerceclass-wc-gateway-bacs.php
核心文件,使用订单商品中的特定变体属性值在 Woocommerce 订单接收页面中显示匹配的银行账户(谢谢):
add_filter( 'woocommerce_bacs_accounts', 'filter_woocommerce_bacs_accounts_callback', 10, 1 );
function filter_woocommerce_bacs_accounts_callback( $bacs_accounts ){
if ( empty($bacs_accounts) ) {
return $bacs_accounts; // Exit
}
if( is_wc_endpoint_url('order-received') ) {
$endpoint = 'order-received';
} elseif( is_wc_endpoint_url('view-order') ) {
$endpoint = 'view-order';
} else {
return $bacs_accounts; // Exit
}
// Get the WC_Order Object
$order = wc_get_order( get_query_var($endpoint) );
$sort_codes = []; // Initializing variable array
// Loop through order items
foreach ( $order->get_items() as $item ) {
$sort_codes[] = $item->get_meta("pa_sede");
}
if ( empty($sort_codes) ) {
return $bacs_accounts; // Exit
}
// Loop through Bacs accounts
foreach ( $bacs_accounts as $key => $bacs_account ) {
$bacs_account = (object) $bacs_account;
// Remove the non matching bank accounts
if ( ! in_array($bacs_account->sort_code, $sort_codes ) ) {
unset($bacs_accounts[$key]);
}
}
return $bacs_accounts;
}
代码进入function.php
您的活动子主题(或活动主题)的文件中。经过测试并可以工作(它应该与您的产品属性一起使用以进行变体pa_sede
)。
推荐阅读
- string - Numpy 整数数组到没有“0x”的十六进制字符串数组
- jquery - 使用 MVC asp.net 和 jquery 的失败复选框
- python - 如何让我的不和谐机器人回复提及?
- tkinter - 从命令行正确使用 tkinter 项目中的 pyInstaller
- flutter - 只是一般性问题 - Flutter Xclint 警告等
- spelling - Chrome 找到拼写错误的单词并更改为正确的单词后,Chrome 是否可以将错误的拼写及其对应的单词保存到文档中?
- javascript - Highcharts - 当我启用另一个系列时如何禁用一个系列?
- git - Git pull 无法锁定 ref,永久解决方案?
- elasticsearch - ElasticSearch 没有在字段 [field_name] 上声明的类型 [search_as_you_type] 的处理程序
- ios - 如何将 IAP 日期转换为日期?