I would like to show the stock status (eg. In Stock / Out of Stock) for each product variation shown in the drop down list of variations on the Woocommerce Product Page. I have copied the relevant function to my theme's functions.php file, and can edit the content, but am unsure how to pull out the required stock status for each variation.
// Updated Woocommerce Product Variation Select if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) { /** * Output a list of variation attributes for use in the cart forms. * * @param array $args * @since 2.4.0 */ /* function wc_dropdown_variation_attribute_options( $args = array() ) { $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array( 'options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '', 'show_option_none' => __( 'Choose an option', 'woocommerce' ), ) ); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $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' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options. if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { $attributes = $product->get_variation_attributes(); $options = $attributes[ $attribute ]; } $html = ''; $html .= '' . esc_html( $show_option_none_text ) . ''; if ( ! empty( $options ) ) { if ( $product && taxonomy_exists( $attribute ) ) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) ); foreach ( $terms as $term ) { if ( in_array( $term->slug, $options ) ) { $html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' '; } } } else { foreach ( $options as $option ) { // This handles lt 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); $html .= '' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . ' Output Stock Details Here '; } } } $html .= ''; echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); } }
I can pull out the stock level for the overall product, but now for each variation.
Any help would be greatly appreciated.
Update July 2018 (changed a bad variable name. Enabled for variable products with 1 dropdown)
I have tried the code of Ali_k, but it was not working in my test server for some variable products.
So I have maid some changes to the Ali_k's code to get this working in my test server (with last WooCommerce version):
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and work in variable products that have only one attribute for variations…
With the code of Ali_K, Here an example of a wrong displayed text for a variable product with multiple select fields (so multiple attributes for variations):
Ok, first you'll need to get product variations like this:
And inside options loop, you need to loop through the variations and find the current option stock status
Outside the variations loop you need to add the wording for in-stock and out-of-stock variations
Then change the html to include an additional variable ($stock_content)
So a complete function will look like this: