woocommerce_before_stock_status
Fires before the stock status is displayed for a product, allowing for custom stock status modifications.
add_action( 'woocommerce_before_stock_status', $callback, 10, 1 );
Description
Fires before the WooCommerce stock status is displayed for products. Use this action to add custom content or modify the stock status output, especially for custom product types or specific inventory management scenarios. It's important to check if the product is of the expected type before implementing custom logic.
Usage
add_action( 'woocommerce_before_stock_status', 'your_function_name', 10, 1 );
Examples
add_action( 'woocommerce_before_stock_status', 'my_custom_woocommerce_before_stock_status_logic', 10, 0 );
/**
* Example function hooked into woocommerce_before_stock_status.
*
* This function demonstrates adding custom content or logic right before
* the default stock status is displayed on a WooCommerce product page.
* In a real-world scenario, you might check for specific product conditions
* or user roles to conditionally display information.
*/
function my_custom_woocommerce_before_stock_status_logic() {
// Access the global product object if it's available.
global $product;
// Example: Check if the product is on sale and display a special notice.
if ( $product && $product->is_on_sale() ) {
echo '<p class="custom-sale-notice">Special offer! This product is currently on sale.</p>';
}
// Example: Conditionally display a message based on user login status.
if ( is_user_logged_in() ) {
// You could fetch user-specific data here.
$current_user = wp_get_current_user();
echo '<p>Welcome back, ' . esc_html( $current_user->display_name ) . '! Check out our latest deals.</p>';
} else {
echo '<p>Log in to see exclusive member pricing.</p>';
}
}
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:297
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' );
}
}
}