Ẩn thanh toán khi không chọn Phương Thức Giao Hàng

Removing Checkout Button When Shipping is Not Available

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.
(WooCommerce 2.4+)

This also includes users who have enabled the multiple shipping packages feature available in WooCommerce 2.1+. It will check each package, and if any of them are missing shipping options, the button will be removed.

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

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' );

I ended up solving this by not hiding the button, but by forcing an error if there is no shipping. I’m using the woocommerce_checkout_process action for this:

function is_valid_shipping() {
    $selected_shipping = WC()->session->get('chosen_shipping_methods');
    if(in_array(false, $selected_shipping)){
        wc_add_notice( __( 'Your Shipping is not valid.' ), 'error' );
    }
}
add_action( 'woocommerce_checkout_process', 'is_valid_shipping' );

or

add_action( 'woocommerce_after_checkout_validation', function( $data, $errors ) {
	if ( true ) { // Change this to your condition which checks if there are shipping options.
		$errors->add( 'invalid shipping', 'No shipping methods available.' ); // You can change this message to anything you'd like.
	}
}, 10, 2 );
1 Like

Nếu như mặt hàng e bán là digital, thì có cách nào ẩn thanh toán đi và hiện nút download cho các sản phẩm free không ạ ? (yêu cầu đăng nhập để tải xuống tệp tin)

E chỉ cần chọn mục sản phẩm ảo thì mọi thứ đã được ẩn đi nhé.

1 Like