隐藏Woocommerce中特定付款方式的下订单按钮

问题描述

如何仅对“检查”网关禁用“下订单”按钮。我不希望我的用户为该网关下订单,因为他们需要先通过给定的信息进行联系,然后才能进行任何付款。

我找到了Remove Woocommerce "place order" button for a specific shipping class,但我想使用“支票”付款方式。

我尝试将ID 332替换为“ Cheque” ID,但它完全删除了所有网关的按钮。后端的ID为cheque,而结帐页面的ID和类为payment_method_cheque

add_filter('woocommerce_order_button_html','remove_order_button_html' );
function remove_order_button_html( $button ) {
    // HERE define your targeted shipping class
    $targeted_payment_method = 'payment_method_cheque';
    $found = false;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
            $found = true; // The targeted shipping class is found
            break; // We stop the loop
        }
    }

    // If found we remove the button
    if( $found )
        $button = '';

    return $button;
}

但是它不起作用。有什么建议吗?

解决方法

更新:它比您想象的要复杂一些,但是需要一些jQuery来刷新结帐…请尝试以下操作:

function refresh_update_qty() {
    if (is_cart() || is_product()):
        ?>
        <script type="text/javascript">
            (function($) {

            $('.woocommerce').on('click',' button.plus,button.minus',function(e){
                var math_method = '';
                if($(this).hasClass('plus')) {
                    math_method = "1";
                }else if($(this).hasClass('minus')) {
                    math_method = "-1";
                } else {
                //    Do nothing
                }
                var this_input = this.parentNode.querySelector('input[type=number]');
                var current_val = this_input.value;
                var new_val = parseInt(current_val) + parseInt(math_method);
                this_input.value = new_val;
                <?php
                if(is_cart()):
                ?>
//                IF CART PAGE UPDATE CART
                document.getElementById('update_cart_button').disabled = false;
                $("[name='update_cart']").trigger("click");
                <?php
                endif;
                ?>
                e.preventDefault();
            });
            })( jQuery );

        </script>
        <?php
    endif;
}
add_action( 'wp_footer','refresh_update_qty' );

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

相关:Change Pay button on checkout based on Woocommerce chosen payment method