I am trying to remove the proceed to checkout button and restrict access to the checkout page until a customer fills out the ‘Calculate shipping’ option on the basket page.
I created a local shipping method that’s restricted to multiple post/zipcodes. I then went and added this to my functions.php file.
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 );
However when I test the site in different browsers and in incognito modes the ‘Proceed to checkout’ button is there.
If I click on the ‘Calculate shipping’ link and not fill out the form but update it the button disappears. I basically want the button to appear when a customer fills out the ‘Calculate shipping’ form on the cart page (and have one of the postcodes in my shipping method) before being able to proceed to the checkout page.
2
Answers
You should better try with the “Chosen shipping method” from WooCommerce session like:
Or in a different way using
woocommerce_check_cart_items
action hook:It should better work.
The code above didn’t work ($chosen_shipping_methods is an array and if it has more than 0 elements this condition will always be true because $chosen_shipping_methods is an array that is initialized with a default value of array(”) in WooCommerce), so I rejigged it: