php - 在 WooCommerce 中仅保留本地取件运输方式的特定付款方式
问题描述
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
// When 'local delivery' has been chosen as shipping rate
if ( in_array( 'local_delivery', $chosen_shipping_rates ) ) :
// Remove bank transfer payment gateway
unset( $gateways['bacs'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
这段代码对我不起作用。我怎样才能做到这一点?
解决方案
请尝试以下操作:
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways', 10, 1 );
function my_custom_available_payment_gateways( $available_gateways ) {
if( is_admin() ) return $available_gateways; // Only for frontend
$local_found = false; // Initializing
// Loop through chosen shipping method rates
foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $chosen_shipping_rate_id ) {
if( strpos( $chosen_shipping_rate_id, 'local_pickup' ) !== false || strpos( $chosen_shipping_rate_id, 'local_delivery' ) !== false ) {
$local_found = true; // Local pickup/delivery found
break; // Stop the loop
}
}
// If Local pickup/delivery is the chosen shipping method
if( $local_found ) {
// Loop through available payment methods
foreach ( $available_gateways as $payment_method_id => $payment_label ) {
// Remove all payement methods except "cheque"
if ( 'cheque' !== $payment_method_id ) {
unset( $available_gateways[$payment_method_id] );
}
}
}
return $available_gateways;
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
推荐阅读
- c# - ASP.NET WebForm - 按字符串获取表格单元格
- python - ModuleNotFoundError:没有名为“xlswriter”的模块
- ms-access - 有没有办法保存访问宏中变量的查找值?
- python - 如何从硒中的搜索下拉列表中选择值
- azure-logic-apps - 如何在 CREATE BLOB 操作上创建目录结构
- c# - 即使我只是在初始化,为什么会出现此错误?
- c# - 如何为基于超类值验证的值对象创建基类/结构?
- ray - 如何比使用 `ray.tune.Trainable` 评估更频繁地记录
- swift - 在 ARImageTrackingConfiguration 中同时跟踪两个对象
- r - 饼图标签重叠