Skip to content
Advertisement

Display shipping class name on WooCommerce single product pages

Need to display the product shipping class to product page! any ideas why this is not working?

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

    $term = get_term_by( 'slug', $product->get_product_shipping_class(), 'product_shipping_class' );

    if( is_a($term, 'WP_Term') && $term->name == $product_shipping_class ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
}

Advertisement

Answer

There are multiple mistakes in your code. Try the following instead:

add_action('woocommerce_single_product_summary', 'display_product_shipping_class', 15 );
function display_product_shipping_class(){
    global $product;
    
    $shipping_class = $product->get_shipping_class();

    if( ! empty($shipping_class) ) {
        $term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
    
        if( is_a($term, 'WP_Term') ){
            echo '<p class="product-shipping-class">' . $term->name . '</p>';
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement