How to add variation stock status to Woocommerce product variation dropdown

后端 未结 4 879
灰色年华
灰色年华 2020-11-27 08:17

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 h

相关标签:
4条回答
  • 2020-11-27 09:02

    Ok, first you'll need to get product variations like this:

    $variations = $product->get_available_variations();
    

    And inside options loop, you need to loop through the variations and find the current option stock status

    foreach ($variations as $variation) {
        if($variation['attributes'][$name] == $option) {
            $stock = $variation['is_in_stock'];
    
        }
    }
    

    Outside the variations loop you need to add the wording for in-stock and out-of-stock variations

    if( $stock == 1) {
        $stock_content = ' - In stock';
    } else {
        $stock_content = ' - Out of stock';
    }
    

    Then change the html to include an additional variable ($stock_content)

    $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option  .  $stock_content ) . '</option>'; 
    

    So a complete function will look like this:

    add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
    function show_stock_status_in_dropdown( $html, $args ) {
        $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' ); 
    
      // Get all product variations
        $variations = $product->get_available_variations();
    
        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 ) ) { 
              // 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 .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>'; 
                    } 
                }
            } else {
                foreach ( $options as $option ) {
                        foreach ($variations as $variation) {
                            if($variation['attributes'][$name] == $option) {
                                $stock = $variation['is_in_stock'];
                            }
                        }       
                    if( $stock == 1) {
                        $stock_content = ' - (In Stock)';
                    } else {
                        $stock_content = ' - (Out of Stock)';
                    }
                     // This handles < 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 .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option  .  $stock_content ) . '</option>'; 
    
                }
            } 
        } 
    
        $html .= '</select>'; 
    
        return $html;
    }
    
    0 讨论(0)
  • 2020-11-27 09:11

    I adapted your code so that only in stock variations display in the dropdown. I made $stock return bool, then

    if ($stock_status){
                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) /*. $stock_status*/ ) . '</option>';
                }
    

    If you can think of a better way to do this It would be much appreciated. P.S thank you for this solution, I can't believe this isn't default woocommerce behaviour to hide out of stock variants.

    0 讨论(0)
  • 2020-11-27 09:12

    Update 2019 (Only for variable products with 1 dropdown)

    Anyway this will really work when there is ONLY ONE dropdown select field (so one attribute for the variations).

    With multiple attributes (so multiple dropdown select fields) it displays something that can be wrong depending on the variations stock status attributes terms combination.

    See the screenshot at the end that shows a wrong display case…

    I have tried the code of Ali_k, but it was not working in my test server when variable products have multiple dropdowns.

    So I have made some changes to the Ali_k's code to get this working in my test server (with last WooCommerce version).

    To handle backorders see: Add backorders stock status to Woocommerce variable product dropdown

    The code:

    // Function that will check the stock status and display the corresponding additional text
    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)';
    }
    
    // The hooked function that will add the stock status to the dropdown options elements.
    add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
    function show_stock_status_in_dropdown( $html, $args ) {
        // Only if there is a unique variation attribute (one dropdown)
        if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
    
        $options               = $args['options'];
        $product               = $args['product'];
        $attribute             = $args['attribute']; // The product attribute taxonomy
        $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 ) ) {
                        // HERE Added the function to get the text status
                        $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 );
                    // HERE Added the function to get the text status
                    $stock_status = get_the_stock_status( $product, $name, $option );
                    $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
                }
            }
        }
        $html .= '</select>';
    
        endif;
    
        return $html;
    }
    

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

    0 讨论(0)
  • 2020-11-27 09:14

    @LoicTheAztec

    I have been using your updated code with the backorder function replacement too but come across an error due to a non-existent function call.

    get_the_stock_status() is now get_stock_status() is it not ?

    Anyhow, the updated code below fixes the issue and an issue I had with a gift card plugin that was failing to load due to the above error.

    // Function that will check the stock status and display the corresponding additional text
    function get_stock_status_text( $product, $name, $term_slug ){
        foreach ( $product->get_available_variations() as $variation ){
            if($variation['attributes'][$name] == $term_slug ) {
                $is_in_stock = $variation['is_in_stock'];
                $backordered = get_post_meta( $variation['variation_id'], '_backorders', true );
                $stock_qty   = get_post_meta( $variation['variation_id'], '_stock', true );
                break;
            }
        }
        $stock_status_text = $is_in_stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
        return $backordered !== 'no' && $stock_qty <= 0 ? ' - (On Backorder)' : $stock_status_text;
    }
    
    // The hooked function that will add the stock status to the dropdown options elements.
    add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
    function show_stock_status_in_dropdown( $html, $args ) {
    
        // Only if there is a unique variation attribute (one dropdown)
        if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
    
        $options               = $args['options'];
        $product               = $args['product'];
        $attribute             = $args['attribute']; // The product attribute taxonomy
        $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 ) ) {
                        // HERE Added the function to get the text status
                        $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 );
                    // HERE Added the function to get the text status
                    $stock_status = get_stock_status( $product, $name, $option );
                    $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
                }
            }
        }
        $html .= '</select>';
    
        endif;
    
        return $html;
    }
    
    0 讨论(0)
提交回复
热议问题