Php 禁用";购物车需要付款”;对于Woocommerce中的特定优惠券代码

Php 禁用";购物车需要付款”;对于Woocommerce中的特定优惠券代码,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,我需要隐藏信用卡付款时,我有一个特定的优惠券,如“tcrfam”,当我使用任何不同的显示卡付款时,我的想法是,我不给优惠券的100%或免费的,没有情况下,我问信用卡数据 请参见示例: 我尝试了此代码,但不起作用: add_action('woocommerce_before_checkout_form', 'apply_product_on_coupon'); function apply_product_on_coupon() { global $woocommerce; $coup

我需要隐藏信用卡付款时,我有一个特定的优惠券,如“tcrfam”,当我使用任何不同的显示卡付款时,我的想法是,我不给优惠券的100%或免费的,没有情况下,我问信用卡数据

请参见示例:

我尝试了此代码,但不起作用:

add_action('woocommerce_before_checkout_form', 'apply_product_on_coupon');
function apply_product_on_coupon() {
  global $woocommerce;
  $coupon_id = 'tcrfam';
  if(in_array($coupon_id, $woocommerce->cart->get_applied_coupons())){    
    echo "Yes the coupon its inserted dd";
    add_filter( 'woocommerce_cart_needs_payment', '__return_false' );
  }
}

我需要使用添加过滤器('woocommerce购物车需要付款','uuuuuReturn\uFalse')在上述代码中的函数中,但我不知道如何操作,有人能告诉我如何操作吗?谢谢

您需要的代码应该直接位于过滤器挂钩中,如:

add_filter( 'woocommerce_cart_needs_payment', 'filter_cart_needs_payment', 10, 2 );
function filter_cart_needs_payment( $needs_payment, $cart  ) {
    // The targeted coupon code
    $targeted_coupon_code = 'tcrfam';

    if( in_array( $targeted_coupon_code, $cart->get_applied_coupons() ) ) {
        $needs_payment = false;
    }
    return  $needs_payment;
}

代码进入活动子主题(或活动主题)的functions.php文件。应该有用。

我发现这段代码对我很有用,但我有一个问题。。。如果禁用付款方式的优惠券代码不止一个?我能用吗

add_filter( 'woocommerce_cart_needs_payment', 'filter_cart_needs_payment', 10, 2 ); function filter_cart_needs_payment( $needs_payment, $cart  ) {
// The targeted coupon code
$targeted_coupon_code = '1code' || '2code';

if( in_array( $targeted_coupon_code, $cart->get_applied_coupons() ) ) {
    $needs_payment = false;
}
return  $needs_payment;
}
我试过了,但Woocommerce并没有问我每个优惠券代码的付款方式,而不仅仅是我写的2

提前谢谢