我尝试在产品类别列表的 WordPress add_filter 指令中隐藏“货到付款”付款(COD)
我有一个调试跟踪,其中写入布尔变量为 true 但是,按照下面的说明,取消设置不会激活 你知道为什么吗?
add_filter('woocommerce_available_payment_gateways', 'remove_cod_payment_gateways_for_some_taxonomy_products',15);
function remove_cod_payment_gateways_for_some_taxonomy_products($available_gateways)
{
//...
if (in_array($term->term_id, $category_ids)){
var_dump("found");
$unset = true;
break;
}
//...
// I am sure $unset == true and found is displayed on the page!!
if ($unset == true) unset($available_gateways['cod']);
return $available_gateways;
}
货到付款仍然可见;我不明白为什么 你有想法吗?
如果我抑制 if ($unset == true) ,则 cod 付款将被隐藏......但我知道条件是 true 。所以我真的不明白
谢谢您的帮助
如果存在属于特定产品类别的购物车商品,您没有使用正确的方法来隐藏“COD”(货到付款)支付网关...请尝试以下操作:
add_filter( 'woocommerce_available_payment_gateways', 'hide_cod_payment_gateway_based_on_categories', 20 );
function hide_cod_payment_gateway_based_on_categories( $payment_gateways ) {
// Only for checkout page
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return $payment_gateways;
// Here define your product categories term Ids, slugs or names.
$targeted_terms = array('hoodies');
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
if ( isset($payment_gateways['cod']) && has_term($targeted_terms, 'product_cat', $item['product_id']) ) {
unset($payment_gateways['cod']);
break;
}
}
return $payment_gateways;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。