How to add custom stock status to products in WooCommerce 4+

Last update: 04/22 – Tested in WordPress 5.9.2 & WooCommerce 6.3.1

Code goes in functions.php file of the active child theme (or active theme).


Use woocommerce_product_stock_status_options

instead of woocommerce_product_options_stock_status.

This way you can immediately add a status instead of replace the existing dropdown


Also use woocommerce_get_availability_text & woocommerce_get_availability_class
opposite woocommerce_get_availability.

This way you don’t have to add the existing statuses again

// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
    // Add new statuses
    $status['pre_order'] = __( 'Pre order', 'woocommerce' );
    $status['contact_us'] = __( 'Contact us', 'woocommerce' );

    return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );

// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
    // Get stock status
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $availability = __( 'Pre order', 'woocommerce' );
        break;
        case 'contact_us':
            $availability = __( 'Contact us', 'woocommerce' );
        break;
    }

    return $availability; 
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
    // Get stock status
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $class="pre-order";
        break;
        case 'contact_us':
            $class="contact-us";
        break;
    }

    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

enter image description here


Use woocommerce_admin_stock_html to display the new stock status on the admin products list table

// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
    // Simple
    if ( $product->is_type( 'simple' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();
    // Variable
    } elseif ( $product->is_type( 'variable' ) ) {
        foreach( $product->get_visible_children() as $variation_id ) {
            // Get product
            $variation = wc_get_product( $variation_id );
            
            // Get stock status
            $product_stock_status = $variation->get_stock_status();
            
            /*
            Currently the status of the last variant in the loop will be displayed.
            
            So from here you need to add your own logic, depending on what you expect from your custom stock status.
            
            By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
            
            - Product should be in stock if a child is in stock.
            - Product should be out of stock if all children are out of stock.
            - Product should be on backorder if all children are on backorder.
            - Product should be on backorder if at least one child is on backorder and the rest are out of stock.
            */
        }
    }
    
    // Stock status
    switch( $product_stock_status ) {
        case 'pre_order':
            $stock_html="<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">" . __( 'Pre order', 'woocommerce' ) . '</mark>';
        break;
        case 'contact_us':
            $stock_html="<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">" . __( 'Contact us', 'woocommerce' ) . '</mark>';
        break;
    }
 
    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );

enter image description here



Optional: if desired, the custom stock status can be used in hooks where you already have access to the $product object or you can use global $product.

1) No access to the $product object, use global $product as is the case for example with the woocommerce_shop_loop_item_title or the woocommerce_single_product_summary hook

function woocommerce_my_callback() {
    // An example based on global $product
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();

        // Use this line during testing, delete afterwards!
        echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
        
        // Compare
        if ( $product_stock_status == 'My custom stock status' ) {
            // Etc..
        }
    }
}
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_my_callback', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_callback', 10 );

2) Access to the $product object, because it’s passed by default to the callback function. As is the case for example with the woocommerce_get_price_html hook

function filter_woocommerce_get_price_html( $price, $product ) {
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();

        // Use this line during testing, delete afterwards!
        echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
        
        // Compare
        if ( $product_stock_status == 'My custom stock status' ) {
            // Etc..
            // $price .= ' my text';
        }
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

Leave a Comment