【问题标题】:Disable "cart needs payment" for a specific coupon code in Woocommerce在 Woocommerce 中禁用特定优惠券代码的“购物车需要付款”
【发布时间】:2020-01-17 15:56:30
【问题描述】:

当我有特定的优惠券(例如“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' );
  }
}

我需要在函数中使用 add_filter ('woocommerce cart needs payment', '__return_false'); 就像上面的代码一样,但我不知道怎么做,谁能给我知道该怎么做吗?谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:

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

    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 文件中。它应该可以工作。

    【讨论】:

      【解决方案2】:

      我发现这个 sn-p 对我很有用,但我有一个问题......如果禁用方法支付的优惠券代码不止一个?可以用吗?

      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 个。

      提前致谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        • 2021-08-08
        • 2011-06-21
        • 1970-01-01
        相关资源
        最近更新 更多