At a WooCommerce talk in WordCamp Dublin I asked about changing a slightly confusing message. Instead of a translation plugin I changed it with code. I feel that it is less confusing now.
On Sunday morning, Luminus O. Alabi, a WooCommerce Happiness Ninja at Automattic, spoke at WordCamp Dublin 2017 and demonstrated how to set up a WooCommerce site.
When he got to the checkout page on the demo site there was a lengthy message that there were no shipping methods to the specified shipping country. The site was set up to only ship to Ireland while his test customer’s shipping country was USA.
There are no shipping methods available. Please double check your address, or contact us if you need any help.
I felt that the message could confuse customers – it makes it seem like they have made a mistake. To reassure them, instead of blaming them, it would be worth rephrasing. I asked whether it was easy to change the message. Luminus suggested a translation plugin – a good recommendation for those not comfortable with code. But I love to code…
Search the source
I spent the rest of the day itching to get home to my laptop to search for the string and experiment with changing it.
I found the string in templates/cart/cart-shipping.php. Thankfully it was available to change via apply_filters(). The ternary operator code looks more confusing than it is – it’s there to allow for a different message when on the cart page or checkout page.
After a quick experiment to ensure that I read the ternary operator correctly I was able to change the message.
The message is displayed when WooCommerce knows the customer’s shipping details e.g. the customer is logged in and has previously placed an order or they have entered their details on the checkout page and have returned to cart page.
To help explain the reason for the message, I included the customer’s shipping country in the message.
I haven’t given the code a thorough test but it might be a starting point for others.
Before and After:
Is there a way to adjust the custom message to create multiple messages for different instances of errors?
The current custom message we have: /** *Custom “No Shipping Method Available” Message */ add_filter( ‘woocommerce_no_shipping_available_html’, ‘my_custom_no_shipping_message’ ); add_filter( ‘woocommerce_cart_no_shipping_available_html’, ‘my_custom_no_shipping_message’ ); function my_custom_no_shipping_message( $message ) { return __( ‘Sorry, your order is too large to provide an automated shipping quote. Please break your order up into multiple orders or contact us for personal assistance and we will be happy to help. Thank you!’ ); }
This is perfect for when people have too many items in their cart for one order. However, the problem is that now when people input an incorrect zip/state match they still receive the same message telling them that their order is too large even when they have one small product in their cart. This is obviously confusing for our customers and results in tons of frustrating phone calls.
Is there a way to create a custom code to show different messages based on the input that the customer is creating (ie. zip/state mismatch, too many items in the cart, etc..)
You can definitely change the message for different situations.
In my example I check the entered country and tell the user that we don’t ship to that country. This is done by calling the WC() function – this returns the WooCommerce object and within that you can access the cart, customer info and other info – see the public members in wp-content/woocommerce/includes/class-woocommerce.php.
For example, if an order is too large you need to check the number of items in the cart. This might be done with: $item_count = WC()->cart->get_cart_contents_count(); and then using $item_count to determine whether the order is okay or too large.
Damien, Thank you for publishing this info. I’ve modified the message see the following code:
/***** following added 6/14/2018 to modify Shipping statement of "Enter your full address to see shipping costs." to change to "Free Shipping to U.S. Destinations only. Contact us for International Shipping Costs"*****/ add_filter( 'woocommerce_cart_no_shipping_available_html', 'nszm_no_shipping_available_html' ); add_filter( 'woocommerce_no_shipping_available_html', 'nszm_no_shipping_available_html' ); function nszm_no_shipping_available_html( $message ) { $country = WC()->customer->get_shipping_country(); if ( !empty( $country ) ) { $all_countries = WC()->countries->get_countries(); return sprintf( "Free Shipping to U.S. Destinations only. Please contact us for International Shipping Costs to ship to %s.", $all_countries[ $country ] ); } return " Please contact us for your International Shipping Costs."; } /***** end of modification *****/
However need to make the phrase of “contact us” linkable to my contact page. I’ve attempted to insert contact us but it just blows up the site. Obviously I don’t know what I am doing. So help. Thanks in advance John
I gave your code a try and I think I know what the problem might be – quotes.
The messages are in double quotes and the href is too so you end up with mismatched quotes. The solution is to put the message in single quotes:
function nszm_no_shipping_available_html( $message ) { $country = WC()->customer->get_shipping_country(); if ( !empty( $country ) ) { $all_countries = WC()->countries->get_countries(); return sprintf( 'Free Shipping to U.S. Destinations only. Please contact us for International Shipping Costs to ship to %s.', $all_countries[ $country ] ); } return 'Please contact us for your International Shipping Costs.'; }
Damien,
Thanks so much for the solution. Loaded it into my function.php and it worked like a champ. Appreciate all your help.
Regards, John
Hi Damien,
Thanks for the amazing tips! However I am facing problem modifying the code myself as I am trying to change the message for specific states of a country rather than countries. I have tried doing it myself but it ultimately broke my site. Could you help me in customising the code? Your help would be greatly appreciated!
Best Regards, JL
@JL: Can you email me directly and include the code you tried and describe what you are trying to do. I will try to help you.
Hello Demien,
You’re a lifesaver! This works like a charm! However, do you how I can use the same function – but for the province instead?
Thanks, Demien!
@Pierre – Try using WC()->customer->get_shipping_state()
return sprintf( “Oops – we don’t ship to %s.”, WC()->customer->get_shipping_state() );
Source code Link has changed, use this permalink for longstanding articles https://github.com/woocommerce/woocommerce/blob/a92da3de46caebfaabd2da0c1dbdac3a2bc0f89d/templates/cart/cart-shipping.php#L67
@Sebastian: Thank you for the permalink. I have updated the post.