Snippet

Display Out-of-Stock Message on WooCommerce Product Page

How to display out of stock message in woocommerceWoocommerce show out of stock on product pageDisplay out of stock text woocommerceWoocommerce out of stock message not showingCustomize out of stock message woocommerceWoocommerce out of stock notificationShow out of stock products woocommerceWoocommerce display out of stock on catalogHow to add out of stock label in woocommerceWoocommerce out of stock message settings

Explanation

To show a custom "out of stock" message on your WooCommerce product page, this code does the trick. It checks if a product is out of stock and displays a friendly message right on the product page.

Here's what it does:

  • Product Page Message: It adds a message saying "Sorry, this product is currently out of stock" directly on the product page when an item isn't available.
  • Catalog Visibility: Out-of-stock products are still shown in your catalog, so customers know they exist even if they can't buy them right now.
  • Catalog Label: In the product catalog, it adds a noticeable "Out of Stock" label to items that aren't available, making it clear at a glance.
  • Custom Styling: The message and label are styled with bold red text to catch the eye, ensuring customers notice the stock status.

This setup helps manage customer expectations by clearly communicating product availability, both on individual product pages and in the broader catalog view.

Code

1<?php 2// Hook into WooCommerce to display a custom out-of-stock message on the product page 3add_action('woocommerce_single_product_summary', 'wp_dudecom_display_out_of_stock_message', 20); 4 5function wp_dudecom_display_out_of_stock_message() { 6 global $product; 7 8 // Check if the product is out of stock 9 if (!$product->is_in_stock()) { 10 // Display a custom out-of-stock message 11 echo '<p class="stock out-of-stock">' . esc_html__('Sorry, this product is currently out of stock.', 'woocommerce') . '</p>'; 12 } 13} 14 15// Ensure out-of-stock products are displayed in the catalog 16add_filter('woocommerce_product_query_meta_query', 'wp_dudecom_include_out_of_stock_products_in_catalog'); 17 18function wp_dudecom_include_out_of_stock_products_in_catalog($meta_query) { 19 // Remove the default meta query that hides out-of-stock products 20 foreach ($meta_query as $key => $query) { 21 if (isset($query['key']) && '_stock_status' === $query['key']) { 22 unset($meta_query[$key]); 23 } 24 } 25 return $meta_query; 26} 27 28// Add a custom label to out-of-stock products in the catalog 29add_action('woocommerce_before_shop_loop_item_title', 'wp_dudecom_add_out_of_stock_label', 10); 30 31function wp_dudecom_add_out_of_stock_label() { 32 global $product; 33 34 // Check if the product is out of stock 35 if (!$product->is_in_stock()) { 36 // Display a custom out-of-stock label 37 echo '<span class="out-of-stock-label">' . esc_html__('Out of Stock', 'woocommerce') . '</span>'; 38 } 39} 40 41// Enqueue custom styles for the out-of-stock message and label 42add_action('wp_enqueue_scripts', 'wp_dudecom_enqueue_custom_styles'); 43 44function wp_dudecom_enqueue_custom_styles() { 45 wp_add_inline_style('woocommerce-general', ' 46 .stock.out-of-stock { 47 color: #ff0000; 48 font-weight: bold; 49 } 50 .out-of-stock-label { 51 background-color: #ff0000; 52 color: #ffffff; 53 padding: 5px; 54 display: inline-block; 55 margin-top: 10px; 56 } 57 '); 58} 59?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Access your WordPress dashboard.
  2. Navigate to Appearance > Theme File Editor if you are adding the code to functions.php. Alternatively, use a code editor if you are working with a custom plugin file.
  3. Locate the functions.php file from the list of theme files on the right side, or open your custom plugin file.
  4. Copy and paste the provided code snippet into the file.
  5. Save the changes to the file.
  6. Visit your WooCommerce product pages and catalog to verify that the out-of-stock message and label are displayed correctly.

If you encounter any issues or need further assistance with this implementation or more advanced functionality, consider reaching out to wp-dude.com for expert help.