In your online store, you’ll see standard shipping options like Flat rate, free shipping, and Local pickup on the cart and checkout page. After an order is placed you can change the label name of these shipping methods shown on the order page and in emails. Based on your business needs, you can change the label name to something clearer and more understandable to customers. For example, instead of showing the default ‘flat rate’ shipping method it is, the shipping label will be changed to ‘Express Delivery’ providing a meaningful representation of the shipping method.
This post will guide you on how to Change the WooCommerce Shipping “Via” Text for a Specific Shipping Method.
Where to Add Custom Code in WooCommerce?
It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
Solution: Change the WooCommerce Shipping “Via” text for a Specific Shipping Method
This code snippet will filter the WooCommerce shipping methods to replace the flat rate shipping method with Express Delivery. This means that when a customer chooses the flat rate shipping method, the order details and emails will display “Express Delivery” instead of “via Flat Rate.”
// Change shipping method text and "Via" text in WooCommerce orders and emails based on flat rate add_filter('woocommerce_order_shipping_to_display', 'ts_custom_shipping_method_text', 10, 3); add_filter('woocommerce_get_order_item_totals', 'ts_custom_shipping_method_email_text', 10, 3); add_filter('woocommerce_order_shipping_method', 'ts_custom_via_text', 10, 2); function ts_custom_shipping_method_text($shipping_method, $order, $is_html) { // Check if the order has a flat rate shipping method if ($order && is_a($order, 'WC_Order')) { $flat_rate_shipping_methods = array('flat_rate'); // Add other flat rate methods if needed foreach ($order->get_shipping_methods() as $shipping_item) { if (in_array($shipping_item->get_method_id(), $flat_rate_shipping_methods)) { // Get the shipping cost for Express Delivery $shipping_cost = $shipping_item->get_total(); // Change the shipping method text to "Express Delivery" $shipping_method = __('Express Delivery ($' . $shipping_cost . ')', 'woocommerce'); break; } } } return $shipping_method; } function ts_custom_shipping_method_email_text($total_rows, $order, $tax_display) { // Check if the order has a flat rate shipping method if ($order && is_a($order, 'WC_Order')) { $flat_rate_shipping_methods = array('flat_rate'); // Add other flat rate methods if needed foreach ($order->get_shipping_methods() as $shipping_item) { if (in_array($shipping_item->get_method_id(), $flat_rate_shipping_methods)) { // Get the shipping cost for Express Delivery $shipping_cost = $shipping_item->get_total(); // Change the shipping method text to "Express Delivery" $total_rows['shipping']['value'] = __('Express Delivery', 'woocommerce') . ' ($' . $shipping_cost . ')'; break; } } } return $total_rows; } function ts_custom_via_text($via_text, $order) { // Check if the order has a flat rate shipping method if ($order && is_a($order, 'WC_Order')) { $flat_rate_shipping_methods = array('flat_rate'); // Add other flat rate methods if needed foreach ($order->get_shipping_methods() as $shipping_item) { if (in_array($shipping_item->get_method_id(), $flat_rate_shipping_methods)) { // Change the "Via" text to "Express Delivery" $via_text = __('Express Delivery', 'woocommerce'); break; } } } return $via_text; }
Output
Use Case 1: Change the Shipping “Via” Text in the WooCommerce View Order Page
The below output demonstrates that the shipping method “Shipping Via” Text has changed to “Express Delivery” in the WooCommerce View Order Page.
Use Case 2: Change the Shipping “Via” Text in the WooCommerce Emails
When the customer places an order for any product, the shipping method for a flat rate will be displayed as “Express Delivery” in WooCommerce email notifications.
Code Explanation
Here is the explanation of the code step-by-step
1. Custom Shipping Method Text:
This function is hooked to the woocommerce_order_shipping_to_display filter. It receives three parameters: $shipping_method (the current shipping method text), $order (the WooCommerce order object), and $is_html (a boolean indicating if the text is HTML).
It checks if the order has a flat-rate shipping method (flat_rate), and if so, it retrieves the shipping cost for that method and updates the shipping method text to “Express Delivery ($shipping_cost)”.
2. Custom Shipping Method Email Text:
This function is hooked to the woocommerce_get_order_item_totals filter. It receives three parameters: $total_rows (an array containing order item totals), $order (the WooCommerce order object), and $tax_display (a string indicating how taxes are displayed).
Similar to the first function, it checks if the order has a flat rate shipping method, retrieves the shipping cost, and updates the shipping method text in the email order totals.
3. Custom Via Text:
This function is hooked to the woocommerce_order_shipping_method filter. It receives two parameters: $via_text (the current “Via” text) and $order (the WooCommerce order object).
Once again, it checks if the order has a flat rate shipping method and updates the “Via” text to “Express Delivery.”
Conclusion
This code snippet customizes the display of shipping details in WooCommerce orders and emails specifically for the flat rate shipping method. Additionally, you can also customize to hide the Shipping Via text from thank you and view order page in WooCommerce.
Let us know your feedback on how the code was useful or any other queries in the comments section.