Home >Backend Development >PHP Tutorial >How to Display Stock Status in WooCommerce Product Variation Dropdown?

How to Display Stock Status in WooCommerce Product Variation Dropdown?

Barbara Streisand
Barbara StreisandOriginal
2024-10-31 14:37:02948browse

How to Display Stock Status in WooCommerce Product Variation Dropdown?

How to Display Stock Status in WooCommerce Product Variation Dropdown

Question:

How can I display the stock status (In Stock/Out of Stock) for each product variation in the dropdown list on the WooCommerce Product Page?

Answer:

Updated 2021 (Limitations):

The following code only works for variable products with one dropdown (one attribute). For products with multiple attributes and dropdowns, the stock status may be displayed incorrectly.

Modified Code for One Dropdown Variation:

<code class="php">// Function that checks stock status and adds text to the dropdown option
function get_stock_status_text($product, $name, $term_slug) {
    foreach ($product->get_available_variations() as $variation) {
        if ($variation['attributes'][$name] == $term_slug) {
            $stock = $variation['is_in_stock'];
            break;
        }
    }
    return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}

// Function that filters the dropdown options and adds stock status
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown($html, $args) {
    // Only for products with one variation attribute (one dropdown)
    if (sizeof($args['product']->get_variation_attributes()) == 1) {
        $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');

        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)) {
                        $stock_status = get_stock_status_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_status) . '</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);
                    $stock_status = get_stock_status_text($product, $name, $option);
                    $html .= '<option value="' . esc_attr($option) . '"' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option) . $stock_status) . '</option>';
                }
            }
        }
        $html .= '</select>';
    }

    return $html;
}</code>

Note:

  • Add this code to your functions.php file or a custom plugin.
  • Replace "attribute" with the actual name of your variation attribute.

Limitations:

  • This code only works for products with one dropdown variation (one attribute).
  • For products with multiple attributes and dropdowns, the stock status may be displayed incorrectly.

The above is the detailed content of How to Display Stock Status in WooCommerce Product Variation Dropdown?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn