Home > Article > Backend Development > How to Display Product Variation Stock Status in WooCommerce Dropdown?
This tutorial addresses the need to show the stock status (In Stock/Out of Stock) of product variations within the dropdown list on the WooCommerce product page. By modifying a core WooCommerce function, we can retrieve the stock status information for each variation and display it alongside the variation options.
<code class="php">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 ) ) { // Add the 'get_stock_status' function to retrieve and display the stock status $stock_status = get_stock_status( $product, $attribute, $term->slug ); $html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' ' . $stock_status . ' '; } } } 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 ); // Add the 'get_stock_status' function to retrieve and display the stock status $stock_status = get_stock_status( $product, $attribute, $option ); $html .= '' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . ' ' . $stock_status . ' '; } } } $html .= ''; echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); }</code>
<code class="php">// Retrieve and display the stock status of a specific product variation function get_stock_status( $product, $attribute, $term ) { foreach ( $product->get_available_variations() as $variation ) { if ( $variation['attributes'][$attribute] == $term ) { $stock = $variation['is_in_stock'] ? 'In Stock' : 'Out of Stock'; break; } } return ' - (' . $stock . ')'; }</code>
<code class="php">add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options', 10, 2 );</code>
Note: This code assumes that your product has only one attribute for variations. For products with multiple attributes, some modifications may be required to display the stock status correctly in all cases.
The above is the detailed content of How to Display Product Variation Stock Status in WooCommerce Dropdown?. For more information, please follow other related articles on the PHP Chinese website!