2

How to disable the "place an order" button ONLY for the "Cheque" gateway. I don't want my users to place an order for this gateway because they need to make a contact through the given info before making any payments.

I found Remove Woocommerce "place order" button for a specific shipping class which is what I want to do, but for "cheque" payment method instead.

I tried to replace the ID 332 with the "Cheque" ID but it completely removed the button for all gateways. its ID on the backend is cheque and the ID and class on the checkout page 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;
}

But it doesn't work. Any advice?

Titanium
  • 81
  • 1
  • 10

1 Answers1

2

Update: It's a bit less complicated than you thought, but needs some jQuery to refresh checkout… Try the following instead:

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

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related: Change Pay button on checkout based on Woocommerce chosen payment method

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tried it on Woocommerce v3.9.1 first but it didn't do anything. then tried it on the latest version and it removed the button for all gateways. I also tested other IDs like `paypal` or `cod` but it didn't work at all ! It's weird ! I mean why would it at least run the code for `cheque` and wouldn't do the same for other IDs ? what's the difference ? either way, the code doesn't work properly. – Titanium Nov 10 '20 at 11:19