0

I don't want to show all attributes that are available, I just need to show those variations value which have stock saved on back-end. Just like I have a variable named sizes, so i want to show the product as well as its available sizes, if a product has 5 different sizes, but two of its sizes has no stock then display only the other 3 sizes which are in stock, and if one product has 5 different sizes and all sizes are in stock then display all those sizes. Here is the code that I am using:

    echo '<div class="row m-0 justify-content-center">';
    $fabric_values = get_the_terms( $product->id, 'pa_sizes');
    foreach ( $fabric_values as $fabric_value ) {
        echo '<button class="btn btn-circle btn-lg rounded-circle">'."$fabric_value->name" . '</button>';
    }
    echo '</div>';

Here is the snap: See the image here This shows all the attributes which I have stored before. What's the solution, if anybody can help me?

  • If the first product has all these 5 sizes, but size 6 and 7 has no stock available, then size 6 & 7 should not be displayed under available sizes on shop page. – Farhan Ali Jan 15 at 12:13
0

I got an answer from stackoverflow, this works but the all the sizes are being shown in a single button, I want to show all sizes in separate buttons, like the above screenshot has. I know its possible by adding a loop before echoing my button, my loop is not working

    global $product;

    if ( $product->is_type('variable') ) {
        $taxonomy    = 'pa_sizes'; // The product attribute taxonomy
        $sizes_array = []; // Initializing

        // Loop through available variation Ids for the variable product
        foreach( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object

            if( $variation->is_purchasable() && $variation->is_in_stock() ) {
                $term_name = $variation->get_attribute( $taxonomy );
                $sizes_array[$term_name] = $term_name;
            }
        }

        echo '<button class="btn btn-circle btn-lg rounded-circle">' . implode( ', ', $sizes_array ) . '</button>';
    }

So, can anybody add a loop just before echoing the button so that I can display all available sizes in different buttons.

0

This is how I achieved the solution:

    global $product;

    if ( $product->is_type('variable') ) {
        $taxonomy    = 'pa_sizes'; // The product attribute taxonomy
        $sizes_array = []; // Initializing

        // Loop through available variation Ids for the variable product
        foreach( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object

            if( $variation->is_purchasable() && $variation->is_in_stock() ) {
                $term_name = $variation->get_attribute( $taxonomy );
                $file_list[$term_name] = $term_name;

            }
        }

        /*
         * Loop through the array and print all the values
         */
        if(is_array($file_list)){
            sort ($file_list);
            foreach($file_list as $file){ 
                echo '<button class="btn btn-circle btn-lg rounded-circle">' . $file . '</button>';
            }
        }
        else{
            echo '<p style="font-size:12px;">No Sizes Available</p>';
        }

    }

    echo '</div>';
Anyone can get help from this post if having troubles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.