dp926460 2017-09-20 17:30
浏览 80
已采纳

在WooCommerce中隐藏基于产品类型的付款方式

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?

  • 写回答

2条回答 默认 最新

  • dow56114 2017-09-20 20:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用