Search code examples
phpwordpresswoocommercecheckoutshipping-method

Disable “Place order” button for specific shipping method in WooCommerce


I would like to disable the "Place order" button when a specific shipping method is selected, a bit like in "Disable "Place order" button for specific shipping zone in WooCommerce" answer to my previous question, but for a specific Shipping Method instead of a Shipping Zone.

The name of the related shipping method is "LATAM".

Any help is appreciated.


Solution

  • First you need to inspect the shipping methods radio buttons, to find out the shipping method ID corresponding value to "LATAM"…

    enter image description here

    To make it work for a specific shipping method ID you will use the following:

    add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
    function disable_place_order_button_html( $button ) {
        // HERE define your targeted shipping method id
        $targeted_shipping_method = "flat_rate:14";
    
        // 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;
    }
    

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