woocommerce_after_stock_status
Fires after the stock status HTML has been displayed on the product page.
add_action( 'woocommerce_after_stock_status', $callback, 10, 1 );
Description
Fires after WooCommerce displays the stock status on a product page. This hook allows developers to add custom content or modify the existing stock status display, particularly useful for custom product types or conditional stock messaging.
Usage
add_action( 'woocommerce_after_stock_status', 'your_function_name', 10, 1 );
Examples
add_action( 'woocommerce_after_stock_status', 'my_custom_woocommerce_stock_indicator', 10, 1 );
/**
* Displays a custom message after the stock status on WooCommerce product pages.
* This example shows a custom "Limited Stock" message when there are between 1 and 5 items left.
*
* @param WC_Product $product The current WooCommerce product object.
*/
function my_custom_woocommerce_stock_indicator( $product ) {
// Ensure we're on a product page and have a valid product object.
if ( ! is_product() || ! $product instanceof WC_Product ) {
return;
}
// Get the current stock quantity.
$stock_quantity = $product->get_stock_quantity();
// Only display the custom message if stock is limited (between 1 and 5).
if ( $stock_quantity > 0 && $stock_quantity <= 5 ) {
?>
<p class="stock limited-stock"><?php
/* translators: %s: Number of items remaining */
echo sprintf( esc_html__( 'Only %s left in stock!', 'your-text-domain' ), $stock_quantity );
?></p>
<?php
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/integrations/woocommerce/class-woo-automator-codes.php:302
public function show_out_of_stock() {
global $product;
// Make sure it's our custom product type.
if ( $this->product_type === $product->get_type() ) {
if ( SharedFunctionality::get_available_codes_by_group_id( $product->get_id() ) <= 0 ) {
do_action( 'woocommerce_before_stock_status' );
?>
<p class="stock outofstock"><?php esc_html_e( 'Out of stock', 'uncanny-learndash-codes' ); ?></p>
<?php
do_action( 'woocommerce_after_stock_status' );
}
}
}