2

We have multiple shipping classes, and based on each of the shipping classes we would like to display banner/text on the product page.

For example, in a case of free shipping, we would like to display something like "Free shipping on this item". And in a case of "flat Rate" - "Flat rate shipping $10", etc.

I was wondering if there is a plugin or a code that i could include in the functions.php to achieve this.

Thanks!

1
  • Just tried to search for a code on internet. So far no luck.
    – DSingh
    Mar 7, 2018 at 0:55

1 Answer 1

2

To display something above the product page, hook into woocommerce_before_single_product, and to get the shipping class use the get_shipping_class() method on the product, which can be retrieved with wc_get_product():

function wpse_295878_shipping_banner() {
    $product = wc_get_product();

    $shipping_class = $product->get_shipping_class();

    switch ( $shipping_class ) {
        case 'free-shipping':
            echo '<div class="woocommerce-info">Free shipping on this item</div>';
            break;
        case 'flat-rate':
            echo '<div class="woocommerce-info">Flat rate shpping $10</div>';
            break;
    }
}
add_action( 'woocommerce_before_single_product', 'wpse_295878_shipping_banner', 20 );
2
  • Thanks Jacob for providing the code. Can this be placed within functions.php?
    – DSingh
    Mar 7, 2018 at 22:54
  • @DSingh Yes, place the code in your functions.php file.
    – harvey
    Mar 5, 2020 at 7:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.