Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Replace ‘0’ with ‘FREE’ for WooCommerce Shipping Rates on Single Product Page?

Looking to display information about available shipping options on the single product page? The below code retrieves the shipping zones, the base country and city of the shop, and then outputs a table with the names and costs of each shipping method in each zone. If a shipping method has a cost of 0, it replaces the cost with the word “Free.”

function ts_action_woocommerce_after_add_to_cart_form() {
    // get all zones
    $zones = WC_Shipping_Zones::get_zones();

    // get the shop base country
    $base_country = WC()->countries->get_base_country();
    $base_city = WC()->countries->get_base_city();

    // start display of table
    echo '<div>' . __( 'Available Shipping', 'woocommerce' );
    echo '<br><small><span class="shipping-time-cutoff">All orders are shipped from the '.$base_country.'. Order before 12AM Mon-Fri for same day delivery within '.$base_city.'. Order before 3PM Mon-Thu for next day delivery.</span></small>';
    echo '<small><table class="shipping-and-delivery-table">';

    // get name of each zone and each shipping method for each zone
    foreach ( $zones as $zone_id => $zone ) {
        
        echo '<tr><td>';
       
        echo '<strong>' . $zone['zone_name'] . '</strong>' . '</td><td>';

        $zone_shipping_methods = $zone['shipping_methods'];
    
        foreach ( $zone_shipping_methods as $index => $method ) {
            $instance = $method->instance_settings;
            
            // Initialize
            $above = '';
            $output_cost = __( 'Free', 'woocommerce' );

            // Cost isset
            if ( isset( $instance['cost'] ) ) {
                // NOT empty
                if ( ! empty ( $instance['cost'] ) ) {
                    // Output
                    $output_cost = wc_price( $instance['cost'] );   
                } else {
                    // If cost is 0, set to 'Free'
                    $output_cost = __( 'Free', 'woocommerce' );
                }
            }
            
            // Min amount isset
            if ( isset( $instance['min_amount'] ) ) {
                // NOT empty
                if ( ! empty ( $instance['min_amount'] ) ) {
                    // Above
                    $above = __( 'above ', 'woocommerce' );
                    
                    // Output
                    $output_cost = wc_price( $instance['min_amount'] );
                }
            }
            
            echo $instance['title'] . ': ' . $above . '<strong>' . $output_cost . '</strong>' . '<br>';        
        }

        echo '</td></tr>';
    }
        
    echo '</table></small></div>';
}
add_action( 'woocommerce_after_add_to_cart_form', 'ts_action_woocommerce_after_add_to_cart_form', 10, 0 );

Output

If you have assigned any shipping costs to 0, it will be displayed as ‘Free’ on the individual product page. Also, the information includes the base country, base city, and instructions for same-day and next-day delivery.

How to Replace '0' with 'FREE' for WooCommerce Shipping Rates on Single Product Page?

Similarly, without making any changes to the costs as above, simply you can just display shipping costs on product page in WooCommerce. This post will also display the shipping classes and their associated cost on the product page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x