questions

Display product variation stock quantity by color for WooCommerce

Rate this post

I have write a little snippet to get all size product variation color and specific quantity in my product page And I have this result :

color 1 size S = quantity 
Color 1 size M = quantity
Color 1 size L = quantity
Color 2 size M = quantity
Color 2 size L = quantity
...

And I would like a result more like that

Color 1 : quantity of size S, quantity of size M, quantity of size L
Color 2 : quantity of size S, quantity of size M, quantity of size L
...

how can I show this result ?

This is my code:

add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
    global $product;


    if ($product->is_type( 'variable' ) && has_term( 71 , 'product_cat' ))
    {
        echo 'Taille(s) proposée(s) '.'</br>';
        $avail_vari = $product->get_available_variations();
        foreach ($avail_vari as $key => $value)
        {
            $vari_id = $value['variation_id'];
            $vari_color = $value['attributes']['attribute_pa_couleur'];
            sort($vari_color);
            $vari_taille = $value['attributes']['attribute_pa_taille'];
            sort($vari_taille);
            $vari_obj = new WC_Product_variation($vari_id);
            $vari_stock = get_post_meta( $vari_id, '_booking_lockout_field',true );
            $sum+= $vari_stock;
            
            if ( $vari_stock > 0 ) {
                echo $vari_color . " en " . $vari_taille . " : " . $vari_stock . " ".'</br>';
            }
        }
        echo 'Soit un total de : ' . $sum .' costume(s) disponible'.'</br>';
    }
}

 

✔️Solution:

The following should do the trick:

add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
    global $product;

    if ($product->is_type('variable') && has_term(71 , 'product_cat') ) {
        $sum  = 0;
        $data = array();

        // Loop through available variation IDs (visible)
        foreach ( $product->get_visible_children() as $variation_id ) {
            $variation = wc_get_product($variation_id);
            
            $color = $variation->get_attribute('pa_couleur');
            $size  = $variation->get_attribute('pa_taille');
            $stock = $variation->get_meta('_booking_lockout_field');
            
            if ( $stock > 0 ) {
                $sum += $stock;
                $data[$color][] = sprintf( __('%s taille(s) %s'), $stock, $size );
            }
            
        }
        echo __("Taille(s) proposée(s)").' </br>';
        
        foreach ( $data as $color => $values ) {
            echo __("Color ") . $color . ' : ' . implode(', ', $values) . '<br>';
        }
        echo sprintf( __("Soit un total de %s costume(s) disponible(s)"), $sum ) . '</br>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). It should works.

Leave a Reply

Your email address will not be published.

Back to top button