2

如何仅为“支票”网关禁用“下订单”按钮。我不希望我的用户为此网关下订单,因为他们需要在进行任何付款之前通过给定的信息进行联系。

我找到了针对特定运输类别的删除 Woocommerce“下订单”按钮,这是我想要做的,而是用于“支票”付款方式。

我试图用“支票”ID 替换 ID 332,但它完全删除了所有网关的按钮。它在后端的 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;
}

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

4

1 回答 1

2

更新:它没有你想象的那么复杂,但需要一些 jQuery 来刷新结帐……试试下面的方法:

add_filter('woocommerce_order_button_html', 'remove_place_order_button_for_specific_payments' );
function remove_place_order_button_for_specific_payments( $button ) {
    // HERE define your targeted payment(s) method(s) in the array
    $targeted_payments_methods = array('cheque');
    $chosen_payment_method     = WC()->session->get('chosen_payment_method'); // The chosen payment

    // For matched payment(s) method(s), we remove place order button (on checkout page)
    if( in_array( $chosen_payment_method, $targeted_payments_methods ) && ! is_wc_endpoint_url() ) {
        $button = ''; 
    }
    return $button;
}

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。

相关:根据 Woocommerce 选择的付款方式在结帐时更改付款按钮

于 2020-11-09T21:32:01.833 回答