Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Display a Custom Message Based on Product Dimensions in WooCommerce?

Displaying custom messages for oversized products about specific conditions, additional charges or limitations associated with these items allows you to inform such important details directly to customers. The below-given code snippet will help you to display a custom message when the product size goes beyond the dimensions specified in the code.

function ts_display_product_size_information() {
    global $product;

    // Get the 'size' attribute value
    $size = $product->get_attribute('size');

    // Check if the size attribute is set
    if (!empty($size)) {
        echo '<p class="product-size-info">Product Size: ' . nl2br($size) . '</p>';

        // Check if the product size exceeds a certain limit
        $maximum_dimensions = array(100, 40, 40); // Set the maximum dimensions in cm (length, width, height)

        // Convert the size string to an array of integers
        $size_array = array_map('intval', explode('x', $size));

        // Check if any dimension exceeds the maximum
        foreach ($size_array as $key => $dimension) {
            if ($dimension > $maximum_dimensions[$key]) {
                // Product dimension exceeds the maximum, display a custom message
                wc_add_notice('This product size may incur additional shipping charges as it exceeds the normal size limit.', 'notice');
                break; // Stop checking once we find a dimension exceeding the maximum
            }
        }
    }
}

// Hook the function to display on the single product page
add_action('woocommerce_before_single_product', 'ts_display_product_size_information', 20);

Output

When a customer adds a product to their cart, the system checks the dimensions of the selected product. If the size of the product exceeds the specified maximum dimensions set in the code, a custom message is displayed.

How to Display a Custom Message Based on Product Dimensions in WooCommerce? - Tyche Softwares

Similar to the above custom message that is displayed for oversized products, you can also enable or disable shipping based on product dimensions in WooCommerce which will help you to provide special shipping options for such oversized products.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x