1

I would like to remove the "Place order" button when a specific shipping method is selected, using the code of the excellent LoicTheAztec "Disable “Place order” button for specific shipping method in WooCommerce" but excluding the order-pay endpoint.

I tried the following solution but it doesn't work, the Place Order button also disappears on the order-pay endpoint.

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    if ( is_checkout() && ! is_wc_endpoint_url( 'order-pay' ) ) {  
        // HERE define your targeted shipping method id
        $targeted_shipping_method = "table_rate:3";
    
        // Get the chosen shipping method (if it exist)
        $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
        
        // If the targeted shipping method is selected, we disable the button
        if( in_array( $targeted_shipping_method, $chosen_shipping_methods ) ) {
            $style  = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
            $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
            $button = '<a class="button" '.$style.'>' . $text . '</a>';
        }
    }
    return $button;
}

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
fabiocs81
  • 63
  • 1
  • 5

1 Answers1

1

I tested your code and it works for me. The problem is not related to this code.

The hook for the Pay for order button is woocommerce_pay_order_button_html and not woocommerce_order_button_html. You find it in the template: /woocommerce/checkout/form-pay.php

You could remove the ! is_wc_endpoint_url( 'order-pay' ) check because it will never run on that page.

You could check a few things:

  • Is the user who accesses this page logged in?
  • Check your active theme's functions.php file if there are any other functions using the woocommerce_pay_order_button_html hook.
  • Have you installed any plugins that manage / edit this page (besides WooCommerce)?
  • Finally, check that there is no CSS rule hiding the element
Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • 1
    It was a conceptual mistake, I didn't notice the different "place order" hook in the "Order-pay" endpoint. So I just replaced woocommerce_order_button_html with ywraq_quote_button_checkout_html (a hook of "Yith Woocommerce Request a Quote" plugin for the quote request button on checkout), which I need to appear and disappear depending on the shipping method. Thanks so much – fabiocs81 Mar 25 '21 at 21:14
  • 1
    The strange thing was that the woocommerce_order_button_html hook removed the "place order" also in the order-pay endpoint, probably due to a theme or jquery incompatibility (I tried with a default theme and it works) – fabiocs81 Mar 25 '21 at 21:27