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

How to Display Custom Message For Low-Stock Product in WooCommerce Cart?

Improve your customer’s shopping experience and keep them informed about product availability with this handy snippet! It automatically alerts the customers when items in their cart are running low in stock, encouraging prompt purchases and preventing potential disappointment from out-of-stock items.

add_action('woocommerce_before_cart', 'ts_display_low_stock_message');

function ts_display_low_stock_message() {
    // Define the low stock threshold
    $low_stock_threshold = 2;

    // Get the cart
    $cart = WC()->cart;

    // Loop through each cart item
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        // Get product ID
        $product_id = $cart_item['product_id'];

        // Get product object
        $product = wc_get_product($product_id);

        // Check if the product stock is low
        if ($product && $product->get_stock_quantity() !== '' && $product->get_stock_quantity() <= $low_stock_threshold) {
            // Display a custom low stock message
            echo '<p><strong>Low Stock Warning:</strong> The product "' . esc_html($product->get_name()) . '" has low stock. Please consider ordering soon.</p>';
        }
    }
}

Output

Let’s check the stock status of the products in the backend. One product has a stock status of 2, while the other has a stock status higher than 2. Please note that we have set the minimum stock quantity to be 2. If any product in the cart has a stock quantity below this minimum requirement or equal, a warning message will be displayed on the cart page.

How to Display Custom Message For Low-Stock Product in WooCommerce Cart? - Tyche Softwares

When a product with a stock availability of 2 or less is added to the cart, the “Low Stock Warning” message is displayed above the cart table section on the WooCommerce cart page.

How to Display Custom Message For Low-Stock Product in WooCommerce Cart? - Tyche Softwares

For products that have a stock availability of more than 2, only those products the warning message will not be displayed as shown below.

How to Display Custom Message For Low-Stock Product in WooCommerce Cart? - Tyche Softwares

You can also display a custom delivery message for WooCommerce backordered products. This can help display delivery timeframes or any other relevant information specific to the customer’s location

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