php - 根据 WooCommerce 中选择的运输方式隐藏付款方式
问题描述
如果通过将以下代码添加到主题 function.php 来选择一种运输方式,我试图隐藏两种付款方式
// Filter payment gatways for different shipping methods
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:7', $chosen_shipping_rates ) ) {
unset( $gateways['stripe'] );
unset( $gateways['ppec_paypal'] );
}
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways',
'my_custom_available_payment_gateways' );
一切正常。除了我在产品页面上收到此错误。
警告:
in_array() 期望参数 2 是数组,在 [theme function.php 和行号] 中给出 null
解决方案
使用以下方法来防止此错误(也已删除endif;
):
// Filter payment gatways for different shipping methods
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
$chosen_shipping_rates = (array) WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:12', $chosen_shipping_rates ) ) {
unset( $available_gateways['stripe'], $available_gateways['ppec_paypal'] );
}
return $available_gateways;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该有效。
推荐阅读
- typescript - 使用声明命名空间覆盖 node_modules 包的类型
- mlab - Mayavi mlab 故障 - 渲染不会在旋转时更新
- osgi - 如何使用 bnd-maven-plugin 嵌入 maven 依赖项
- javascript - 使用基于类实例类型的模板
- matplotlib - Matplotlib:如何使等方面永久化
- agda - `with fx` 匹配 `false`,但不能构造 `fx == false`
- reactjs - 异步选择反应不加载选项
- compiler-construction - LR(1) 表结构混乱
- sql-server - 更改数据库服务器上现有 SQL 用户的登录名(Windows 用户)
- apache - Apache 2.2.15 在上传某种类型的 pdf 文件时响应状态 44