Custom Text on Product Page based on Shipping Class [closed]
IT Nursery
May 25, 2022
0
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 Answer 1
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 );