Hide payment method based on product type in WooCommerce

守給你的承諾、 提交于 2021-02-16 03:56:53

问题


In WoCommerce, I would like to disable particular payment methods and show particular payment methods for a subscription products in WooCommerce (and vice versa).

This is the closest thing we've found but doesn't do what I am expecting.

Yes, there are plugins that will do this but we want to achieve this without using another plugin and without making our stylesheet any more nightmarish than it already is.

Any help on this please?


回答1:


Here is an example with a custom hooked function in woocommerce_available_payment_gateways filter hook, where I can disable payment gateways based on the cart items (product type):

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $prod_variable = $prod_simple = $prod_subscription = false;
        // Get the WC_Product object
        $product = wc_get_product($cart_item['product_id']);
        // Get the product types in cart (example)
        if($product->is_type('simple')) $prod_simple = true;
        if($product->is_type('variable')) $prod_variable = true;
        if($product->is_type('subscription')) $prod_subscription = true;
    }
    // Remove Cash on delivery (cod) payment gateway for simple products
    if($prod_simple)
        unset($available_gateways['cod']); // unset 'cod'
    // Remove Paypal (paypal) payment gateway for variable products
    if($prod_variable)
        unset($available_gateways['paypal']); // unset 'paypal'
    // Remove Bank wire (Bacs) payment gateway for subscription products
    if($prod_subscription)
        unset($available_gateways['bacs']); // unset 'bacs'

    return $available_gateways;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested on Woocommerce 3+ and works.

This is just an example to show you how things can work. You will have to adapt it




回答2:


This code has been very useful to me, but there is an error in it that I had to fix: the line

 $prod_variable = $prod_simple = $prod_subscription = false;

must be put OUTSIDE (before) the FOREACH otherwise it will reset the flag everytime a new item is executed. I my case, I needed to unset a specific payment method whenever a subscription product was on the cart. As it is, this code will work only if there is just a single subscription product. If I put another different item on cart, the flag will be turn to false again and the payment method will load. Putting the line outside the FOREACH will fix this problem.



来源:https://stackoverflow.com/questions/46328364/hide-payment-method-based-on-product-type-in-woocommerce

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