Enhancing WooCommerce Backend: Displaying Stock for Each Variation in a Variable Product

When managing an online store, it's crucial to have accurate and up-to-date stock information for each variation of a variable product.

This helps ensure that customers can make informed purchasing decisions based on availability, and it enables store owners to effectively manage inventory, prevent overselling, and streamline order fulfillment processes.

1. The woocommerce_admin_stock_html Hook

A. Overview of the woocommerce_admin_stock_html Hook and Its Purpose

The woocommerce_admin_stock_html hook is a powerful tool provided by WooCommerce that allows developers to modify the display of stock information for each variation in a variable product within the WooCommerce backend. By utilizing this hook, you can customize how the stock is presented, add additional details, or apply styling to improve the visibility and management of stock information.

B. Code Example Illustrating the Usage of the woocommerce_admin_stock_html Hook

function display_variations_stock( $stock_html, $the_product ) {

    // do something here

    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'display_variations_stock', 10 ,2 );

2. Stock Variation Display Function

A. Code example

function display_variations_stock( $stock_html, $the_product ) {

    if ( $the_product->get_type() == 'variable' ) {

        $variations     = $the_product->get_children();
        $managing_stock = $the_product->managing_stock();
        $stock_status   = $managing_stock ? $the_product->get_stock_status() : 'outofstock';
        $stock_html     = '';

        if ( ! empty( $variations ) ) {

            $stock_html = ' (';
            foreach ( $variations as $variation_id ) {

                $variation_product = wc_get_product( $variation_id );

                if ( ! $variation_product instanceof \WC_Product ) {
                    continue;
                }

                $variation_stock  = is_null( $variation_product->get_stock_quantity() ) ? 'X' : $variation_product->get_stock_quantity();
                $variation_status = $variation_product->get_stock_status();
                $style            = 'color:#a44';

                switch ( $variation_status ) {
                    case 'instock':
                        if ( ! $managing_stock ) {
                            $stock_status = 'instock';
                        }
                        $style = 'color:#7ad03a';
                        break;
                    case 'onbackorder':
                        if ( ! $managing_stock && 'instock' !== $stock_status ) {
                            $stock_status = 'onbackorder';
                        }
                        $style = 'color:#eaa600';
                        break;
                }

                $stock_html .= sprintf( '<span style="%s">%s</span>, ', $style, $variation_stock );

            }

            $stock_html = substr( $stock_html, 0, - 2 ) . ')';
        }

        switch ( $stock_status ) {

            case 'instock':
                $stock_text = esc_attr__( 'In stock', 'woocommerce' );
                break;

            case 'onbackorder':
                $stock_text = esc_attr__( 'On backorder', 'woocommerce' );
                break;

            default:
                $stock_text = esc_attr__( 'Out of stock', 'woocommerce' );
                break;
        }

        $stock_html = "<mark class='$stock_status'>$stock_text</mark>" . $stock_html;
    }

    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'display_variations_stock', 10 ,2 );

B. Code Explanation

The function begins by checking if the product type is variable using $the_product->get_type(). If it is not a variable product, the original $stock_html is returned without any modifications.

For variable products, we retrieve the child variations of the product using $the_product->get_children(). We then check if stock management is enabled for the product using $the_product->managing_stock(). If stock management is disabled, the stock status is set to 'outofstock' by default, and we initialize the $stock_html variable as an empty string.

If there are variations available for the product, the function enters a loop and iterates over each variation. We retrieve the variation product object using wc_get_product($variation_id). If the variation product is not an instance of \WC_Product, the loop skips to the next iteration. We retrieve the stock quantity of the variation using $variation_product->get_stock_quantity(). If the quantity is null, it is set to 'X'. We also get the stock status of the variation using $variation_product->get_stock_status(). Based on the variation stock status, we set the color style for the stock display using the $style variable. We update the overall $stock_status variable if necessary based on the variation stock status. Finally, we append the formatted stock information for each variation to $stock_html.

After the variation loop, the function uses a switch statement to determine the overall stock status $stock_status and assigns the corresponding localized stock text to $stock_text.

To create the modified stock HTML, the function wraps the $stock_text with a <mark> element and appends the variation stock information from $stock_html.

Finally, the modified $stock_html is returned.