If you want to display the custom message to the customer on the cart page if they have not filled the country, state, and postcode, then keep your close attention on this post as I am going to share how to display the message when country, state, or postcode is not filled in cart page.
This is very simple code snippet to display the custom message on the cart page. Add the following code in Appearance => Editor => functions.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_custom_message_for_incomplete_address' , 10, 1 ); // Alters message on Cart page add_filter( 'woocommerce_no_shipping_available_html', 'wf_custom_message_for_incomplete_address' , 10, 1 ); // Alters message on Checkout page function wf_custom_message_for_incomplete_address( $default ) { $wf_cart_country = WC()->customer->get_shipping_country(); $wf_cart_state = WC()->customer->get_shipping_state(); $wf_cart_postcode = WC()->customer->get_shipping_postcode(); $shipping_msg_array = array(); empty($wf_cart_country) ? $shipping_msg_array[] = 'Country' : ''; empty($wf_cart_state) ? $shipping_msg_array[] = 'State' : ''; empty($wf_cart_postcode) ? $shipping_msg_array[] = 'Postcode' : ''; if( !empty( $shipping_msg_array ) ){ if( count($shipping_msg_array) > 1 ){ $shipping_msg = implode(", ", $shipping_msg_array); return $shipping_msg . " are empty.Please fill up those fields"; }else{ $shipping_msg = $shipping_msg_array[0]; return $shipping_msg . " is empty.Please fill up the field"; } } else return $default; } |
After adding the above snippet in your functions.php file, then your cart page will look like below screenshot when the country, state, or postcode not filled.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Top Useful WooCommerce Code Snippets, woocommerce credit card pyment, woocommerce snippets, woocommerce tutorial, woocommerce tutorials, wordpress woocommerce tutorial