Shipping class message in single product page Woocommerce

We will display a message in the frontend based on the shipping class chosen.

Based on the shipping class slug we will display the class description as a message under the add to cart button. For slug message1 and message2

    
// Function to display the shipping class description on single product page function display_shipping_class_message() { global $product; // Get product shipping classes $shipping_classes = $product->get_shipping_class_id(); // Check if the product has a shipping class and matches the desired slug if ($shipping_classes) { // Get the shipping class object $shipping_class = get_term_by('id', $shipping_classes, 'product_shipping_class'); // Check if the shipping class slug is '7-10-days' if ($shipping_class && ( $shipping_class->slug == 'message1' || $shipping_class->slug == 'message2') ) { // Display the shipping class description echo '<div class="shipping-class-description">'; echo wp_kses_post($shipping_class->description); // Safely output the description echo '</div>'; } } } add_action('woocommerce_after_add_to_cart_form', 'display_shipping_class_message', 11);

Leave a Reply