Disable Proceed To Checkout Softexpert

How to disable / hide proceed to checkout in woocommerce

It’s easy to miss the small notification that appears when no shipping methods are available. This can cause confusion and frustration when the customer continues to the checkout page but cannot complete their order. To help avoid this bothersome situation, to may be a good idea to remove the ‘Proceed to Checkout’ button that appears beneath the subtotal on the cart page. Adding the following code snippet will add an extra check to WooCommerce and remove the button if no shipping options are found.

function disable_checkout_button_no_shipping() {
    $package_counts = array();
     
    // get shipping packages and their rate counts
    $packages = WC()->shipping->get_packages();
    foreach( $packages as $key => $pkg )
        $package_counts[ $key ] = count( $pkg[ 'rates' ] );
 
    // remove button if any packages are missing shipping options
    if( in_array( 0, $package_counts ) )
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );

//Updated Code
function disable_checkout_button_no_shipping() {

    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

	//echo "<PRE>";print_r($chosen_shipping_methods);
    // remove button if there is no chosen shipping method
    if( empty($chosen_shipping_methods[0]) ) {
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    }
	//else
	//{
	//	echo $chosen_shipping_methods[0];
	//	echo "<BR> HERE";
	//}
}
 add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );

Prevent Viewing the Checkout Page, too!

Furthermore, if you wish to prevent access to the checkout page when shipping is unavailable, this similar function should do the trick.

function prevent_checkout_access_no_shipping() {
    // Check that WC is enabled and loaded
    if( function_exists( 'is_checkout' ) && is_checkout() ) {
     
        // get shipping packages and their rate counts
        $packages = WC()->cart->get_shipping_packages();
        foreach( $packages as $key => $pkg ) {
            $calculate_shipping = WC()->shipping->calculate_shipping_for_package( $pkg );
            if( empty( $calculate_shipping['rates'] ) ) {
                wp_redirect( esc_url( WC()->cart->get_cart_url() ) );
                exit;
            }
        }
    }
}
add_action( 'wp', 'prevent_checkout_access_no_shipping' );

Hide Checkout Fields based on cart items

/**
 * Hides checkout fields based on the products in the cart
 *
 * @param  array $fields
 * @return array
 */
function remove_fields_forspecific_products( $fields ) {
    $cart = WC()->cart->get_cart();

    foreach ( $cart as $item_key => $values ) {
        $product = $values['data'];

        if ( $product->id == 742 ) {
			unset( $fields['billing']['first_name'] );
            unset( $fields['billing']['shipping_company'] );
            unset( $fields['billing']['delivery_datetime_field'] );
        }
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'remove_fields_forspecific_products' );

Leave a Reply

Your email address will not be published. Required fields are marked *