Disable “cart needs payment” for a specific coupon code in Woocommerce

假如想象 提交于 2021-01-05 10:13:41

问题


I need to hide the payment by credit card when I have a specific coupon such as "tcrfam" and when I use any different to this show the payment by card, the idea is that I do not give a coupon of 100% or free and there is no case I ask credit card data.

See the example:

I tried this code but dont work:

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' );
  }
}

I need to use add_filter ('woocommerce cart needs payment', '__return_false'); within the function as in the code above but I don't know how to do it, can someone give me an idea of how to do it? Thank you


回答1:


The code that you needs should be in the filter hook directly, like:

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;
}

Code goes in functions.php file of your active child theme (or active theme). It should works.




回答2:


I found this snippet very useful for me, but I've a question ... if the coupon codes for disable the methods payment are more than one? Can I use?

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;
}

I tried it, but Woocommerce is not asking me the payment method for every coupon code and not fot only the 2 that I wrote.

Thanks in advance



来源:https://stackoverflow.com/questions/57963173/disable-cart-needs-payment-for-a-specific-coupon-code-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!