Table of Contents
Displaying Product Variation Stock Status in WooCommerce Dropdown
Overview
Code Implementation
Home Backend Development PHP Tutorial How to Display Product Variation Stock Status in WooCommerce Dropdown?

How to Display Product Variation Stock Status in WooCommerce Dropdown?

Nov 01, 2024 am 10:41 AM

How to Display Product Variation Stock Status in WooCommerce Dropdown?

Displaying Product Variation Stock Status in WooCommerce Dropdown

Overview

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 Implementation

  1. Open your WordPress child theme's functions.php file.
  2. Copy and paste the following modified version of the wc_dropdown_variation_attribute_options function:
<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'          =&gt; false,
        'attribute'        =&gt; false,
        'product'          =&gt; false,
        'selected'         =&gt; false,
        'name'             =&gt; '',
        'id'               =&gt; '',
        'class'            =&gt; '',
        'show_option_none' =&gt; __( '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 ) &amp;&amp; ! empty( $product ) &amp;&amp; ! empty( $attribute ) ) {
        $attributes = $product-&gt;get_variation_attributes();
        $options    = $attributes[ $attribute ];
    }

    $html = '';
    $html .= '' . esc_html( $show_option_none_text ) . '';

    if ( ! empty( $options ) ) {
        if ( $product &amp;&amp; taxonomy_exists( $attribute ) ) {
            // Get terms if this is a taxonomy - ordered. We need the names too.
            $terms = wc_get_product_terms( $product-&gt;get_id(), $attribute, array( 'fields' =&gt; 'all' ) );

            foreach ( $terms as $term ) {
                if ( in_array( $term-&gt;slug, $options ) ) {
                    // Add the 'get_stock_status' function to retrieve and display the stock status
                    $stock_status = get_stock_status( $product, $attribute, $term-&gt;slug );
                    $html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term-&gt;slug, false ) . '&gt;' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term-&gt;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>
Copy after login
  1. Copy and paste the following function below the modified wc_dropdown_variation_attribute_options function:
<code class="php">// Retrieve and display the stock status of a specific product variation
function get_stock_status( $product, $attribute, $term ) {
    foreach ( $product-&gt;get_available_variations() as $variation ) {
        if ( $variation['attributes'][$attribute] == $term ) {
            $stock = $variation['is_in_stock'] ? 'In Stock' : 'Out of Stock';
            break;
        }
    }
    return ' - (' . $stock . ')';
}</code>
Copy after login
  1. Hook the modified wc_dropdown_variation_attribute_options function to the woocommerce_dropdown_variation_attribute_options_html filter:
<code class="php">add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options', 10, 2 );</code>
Copy after login
  1. Save and upload the functions.php file.

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!

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 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)

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

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles