Add variation stock quantity and status to Woocommerce product variation dropdown Add variation stock quantity and status to Woocommerce product variation dropdown wordpress wordpress

Add variation stock quantity and status to Woocommerce product variation dropdown


There are some mistakes in your code and a better way to shows stock quantity + stock status in product variation dropdown.

The first function is a custom function where you will define the stock text addition to be displayed in the product variation dropdown, which is handled by the second function.

In your last function, since Woocommerce 3, get_total_stock() is deprecated and replaced by the method get_stock_quantity(). Also you need to use the variation $product object that is included as an argument in the hooked function.

Note: This will only work for variable products with one dropdown (one defined product attribute for variations)

Here is the revisited code:

// Function that will check the stock status and display the corresponding additional textfunction get_variation_stock_text( $product, $name, $term_slug ){    foreach ( $product->get_available_variations() as $variation ){        if($variation['attributes'][$name] == $term_slug ){            $is_in_stock = $variation['is_in_stock'];            $stock_qty   = get_post_meta($variation['variation_id'], '_stock', true);        }    }    $in_stock     = ' ('.$stock_qty.' ' .__("Skladem", "woocommerce").')';    $out_of_stock = ' ('.__("Vyprodáno", "woocommerce").')';    return $is_in_stock == 1 ? $in_stock : $out_of_stock;}// The hooked function that will add the stock text to the dropdown options elements.add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);function show_stock_status_in_dropdown( $html, $args ) {    // Only if there is a unique variation attribute (one dropdown)    if( sizeof($args['product']->get_variation_attributes()) == 1 ) :    $options               = $args['options'];    $product               = $args['product'];    $attribute             = $args['attribute']; // The product attribute taxonomy    $name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );    $id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );    $class                 = $args['class'];    $show_option_none      = $args['show_option_none'] ? true : false;    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {        $attributes = $product->get_variation_attributes();        $options    = $attributes[ $attribute ];    }    $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';    $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';    if ( ! empty( $options ) ) {        if ( $product && taxonomy_exists( $attribute ) ) {            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );            foreach ( $terms as $term ) {                if ( in_array( $term->slug, $options ) ) {                    // HERE Added the function to get the stock text                    $stock_text = get_variation_stock_text( $product, $name, $term->slug );                    $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_text ) . '</option>';                }            }        } else {            foreach ( $options as $option ) {                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );                // HERE Added the function to get the stock text                $stock_text = get_variation_stock_text( $product, $name, $option );                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' .                esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_text ) . '</option>';            }        }    }    $html .= '</select>';    endif;    return $html;}// Change product availability textadd_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);function filter_product_availability_text( $availability, $product ) {    $stock = $product->get_stock_quantity();    return $product->is_in_stock() ? $stock . ' ' . __("Skladem", "woocommerce") : __("Vyprodáno", "woocommerce");}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Based on "How to add variation stock status to Woocommerce product variation dropdown"