Sometimes is necessary to have a shipping method selected in cart page, before proceeding to checkout.
This can be achieved using a short jQuery function inserted in head via a php function added in functions.php:
// force shipping method in cart
add_action('wp_head','mo_prevent_proceed_to_checkout');
function mo_prevent_proceed_to_checkout() {
echo ' <script>
jQuery(function(){
jQuery(".woocommerce-cart .wc-proceed-to-checkout a.checkout-button").on("click",function(e){
var _this = this;
alert("Please choose your shipping method!");
e.preventDefault();
jQuery(".shipping-calculator-form .button").on("click",function(){
jQuery(_this).off();
});
});
});
</script>';
}
This works by preventing a “default” option, which can be actually a shipping method previously chosen by visitor and remembered via cookie – in which case this is not exactly useful.
However, it can be improved by adding a conditional checking for the current shipping method and outputting the jQuery code only when necessary 🙂