'Display In stock available variations in WooCommerce single product

I have variable products with many variations where only a few items are actually In Stock while the majority of other variations are ''available on backorder''

I would like to be able to display a quick list of ONLY the items that are IN STOCK in the short product description of each product page so the customer doesn't have to try all variations one-by-one to finally find out which ones are in stock. add_filter( 'woocommerce_short_description', 'display_in_stock_variations_to_short_description' ); function display_in_stock_variations_to_short_description( $excerpt ){ global $product;

if ( ! is_product() || empty($product) || ! is_a( $product, 'WC_Product' ) ) 
    return $excerpt;

if( $product->is_type('variable') ) {
    // Loop through visible children
    foreach( $product->get_children() as $variation_id ) {
        $variation = wc_get_product( $variation_id );

        // Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
        if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
            continue;
        }

        // Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price).
        if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $product->get_id(), $variation ) && ! $variation->variation_is_visible() ) {
            continue;
        }

        $max_qty    = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();
        $term_names = []; // Initializing

        // Loop through variation attributes for current varation
        foreach ( $variation->get_variation_attributes() as $attribute => $term_slug ) {
            // Set the term name in an array
            $term_names[] = ucfirst( str_replace( ['-', '_'],[' ', ' '], $term_slug ) );
        }

        if ( $max_qty > 0 ) {
            $excerpt .= sprintf( '<br/>%s: %s %s',
                implode(', ', $term_names),
                $max_qty,
                __('in stock', 'woocommerce')
            );
        }
    }
}
return $excerpt;

}

// Avoid additional content from product short description to be displayed in variation description add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_desscription', 10, 3); function filter_wc_available_variation_desscription( $data, $product, $variation ) { $max_qty = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();

if( $max_qty > 0 )
    $data['variation_description'] = get_post_meta( $variation->get_id(), '_variation_description', true );

return $data;

}

This code displays an inventory, but my website language is Persian and the names of the variables are defined in Persian, and this line does not display the variable name code correctly. How can I fix his language? And I want to translate the word instock into موجود And the next point is that I want a button to be displayed on the page first with the title Show product inventory and when the method clicks, the customer will open a pop-up and display the inventory.



Solution 1:[1]

Here is the soultion with Persian language:

change

// Loop through variation attributes for current varation
    foreach ( $variation->get_variation_attributes() as $attribute => $term_slug ) {
        // Set the term name in an array
        $term_names[] = ucfirst( str_replace( ['-', '_'],[' ', ' '], $term_slug ) );
    }

    if ( $max_qty > 0 ) {
        $excerpt .= sprintf( '<br/>%s: %s %s',
            implode(', ', $term_names),
            $max_qty,
            __('in stock', 'woocommerce')
        );
    }

to

// Loop through variation attributes for current varation
    foreach ( $variation->get_variation_attributes() as $attribute => $term_name ) {
        // Set the term name in an array
        $taxonomy       = str_replace('attribute_', '', $attribute);
        $attribute_name = wc_attribute_label($taxonomy);
        $term_name      = get_term_by( 'slug', $term_name, $taxonomy)->name;
        $term_names[] = $term_name;
    }

    if ( $max_qty > 0 ) {
        $excerpt .= sprintf( '<br/>%s: %s %s',
            implode(', ', $term_names),
            $max_qty,
            __('?????')
        );
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stickman