Each payment method on the WooCommerce Checkout page comes with a description right below the label. What if we could achieve something similar for the shipping rates, so that we can explain to the customer the difference between each method?
This question came up today in our private Slack channel for Woo developers, and we were able to write a quick snippet that does the trick. Here’s the complete code – enjoy!
PHP Snippet: Display Shipping Method Description in the WooCommerce Checkout Page
This code is a WooCommerce snippet that adds a custom description after a specific shipping method on the checkout page. Here’s a breakdown of what each part of the code does:
- add_action(): This function adds an action hook that triggers the bbloomer_shipping_rate_description function after the shipping rate is displayed on the checkout page. The hook used here is woocommerce_after_shipping_rate.
- bbloomer_shipping_rate_description() function: This function defines the custom behavior for displaying the shipping rate description. It takes the shipping method object as a parameter.
- Conditional Check: Inside the function, there’s a conditional check to determine if the shipping method being displayed has a specific ID (free_shipping:6). If it does, a custom HTML message is echoed out to explain the benefits of choosing this shipping rate.
- HTML Output: The HTML output is a simple paragraph tag containing the desired message. This message could contain any information deemed relevant for the specific shipping method identified by its ID.
Overall, this code allows you to customize the display of shipping method descriptions on the WooCommerce checkout page, specifically targeting a shipping method with the ID free_shipping:6.
For more info on how to find your shipping rate IDs, check the โHow to Find Shipping Class IDโ paragraph here:ย https://businessbloomer.com/woocommerce-disable-free-shipping-if-cart-has-shipping-class
/**
* @snippet Shipping Method Description @ WooCommerce Checkout
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 8
* @community Join https://businessbloomer.com/club/
*/
add_action( 'woocommerce_after_shipping_rate', 'bbloomer_shipping_rate_description' );
function bbloomer_shipping_rate_description( $method ) {
if ( $method->id === 'free_shipping:6' ) {
echo '<p>Some HTML in here where you can explain the benefits of choosing this shipping rate</p>';
}
}
Perfect. Thank you men
You’re welcome!