Search code examples
phpwordpresswoocommercetaxonomy-terms

Display the product shipping class on Woocommerce single product pages


I'm trying to display the shipping class on product page but I must do something wrong because nothing shows..

I used the bellow code in function.php:

add_action('woocommerce_single_product_summary', 'display_specific_shipping_class', 15 );
function display_specific_shipping_class(){
    global $product;

    // HERE define your targeted shipping class name
    $defined_shipping_class = "Estimated Delivery in 7-15 days";

    $product_shipping_class = $product->get_shipping_class();

    // Get the product shipping class term name
    $term_name = get_term_by( 'slug', $product_shipping_class, 'product_shipping_class' );

    if( $term_name == $defined_shipping_class ){
        echo '<p class="product-shipping-class">' . $term_name . '</p>';
    }
}

Solution

  • You are getting the WP_term object and not the term name… So instead use the following:

    add_action('woocommerce_single_product_summary', 'display_specific_shipping_class', 15 );
    function display_specific_shipping_class(){
        global $product;
    
        // HERE define your targeted shipping class name
        $defined_shipping_class = "Estimated Delivery in 7-15 days";
    
        // Get the product shipping class WP_Term Object
        $term = get_term_by( 'slug', $product->get_shipping_class(), 'product_shipping_class' );
    
        if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class ){
            echo '<p class="product-shipping-class">' . $term->name . '</p>';
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should works now.