Table of Contents
How to Enhance Variation Stock Status in WooCommerce Variation Dropdown
Updated Function for Single-Attribute Variations (Introduced in 2021)
Additional Functionality: Get Stock Status Text for Each Variation
Home Backend Development PHP Tutorial How to Display Stock Status in WooCommerce Variation Dropdown for Single-Attribute Products?

How to Display Stock Status in WooCommerce Variation Dropdown for Single-Attribute Products?

Oct 30, 2024 pm 02:54 PM

How to Display Stock Status in WooCommerce Variation Dropdown for Single-Attribute Products?

How to Enhance Variation Stock Status in WooCommerce Variation Dropdown

Problem:

When displaying product variations in the WooCommerce Product Page dropdown, the stock status (e.g., In Stock/Out of Stock) for each variation is not readily available.

Solution:

Updated Function for Single-Attribute Variations (Introduced in 2021)

Note: This solution is suitable for variable products with only one dropdown attribute.

Modify the wc_dropdown_variation_attribute_options function:

<code class="php">add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
    // Ensure there's only one variation attribute
    if( sizeof($args['product']-&gt;get_variation_attributes()) == 1 ) {
        $options = $args['options'];
        $product = $args['product'];
        $attribute = $args['attribute'];
        $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
        $show_option_none = $args['show_option_none'] ? true : false;

        if ( ! empty( $options ) ) {
            $html = '&lt;select id=&quot;' . esc_attr( $id ) . '&quot; class=&quot;' . esc_attr( $class ) . '&quot; name=&quot;' . esc_attr( $name ) . '&quot; data-attribute_name=&quot;attribute_' . esc_attr( sanitize_title( $attribute ) ) . '&quot; data-show_option_none=&quot;' . ( $show_option_none ? 'yes' : 'no' ) . '&quot;&gt;';
            $html .= '&lt;option value=&quot;&quot;&gt;' . esc_html( $show_option_none_text ) . '&lt;/option&gt;';

            foreach ( $options as $option ) {
                $stock_status = get_stock_status_text( $product, $name, $option );
                $html .= '&lt;option value=&quot;' . esc_attr( $option ) . '&quot; ' . selected( sanitize_title( $args['selected'] ), $option, false ) . '&gt;' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '&lt;/option&gt;';
            }
            $html .= '&lt;/select&gt;';
        }
    }
    return $html;
}</code>
Copy after login

Additional Functionality: Get Stock Status Text for Each Variation

<code class="php">function get_stock_status_text( $product, $name, $term_slug ) {
    foreach ( $product-&gt;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)';
}</code>
Copy after login

Usage:

Insert the above code snippets into your theme's functions.php file or a plugin file.

Limitations:

  • The provided solution only applies to variable products with one dropdown attribute.
  • For products with multiple dropdown attributes, the stock status display may not be accurate.

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

Statement of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles