questions

How to add a parent class to this function in woocommerce

Rate this post

Below code is for fetching all variations inside a single product and display them in custom location below product summary.

I am able to fetch all the variations, but to add some css to it, i am stuck here

This is what i need to display

<div class="all_variations_thumb">
    <div class="variation_thumb"></div>
    <div class="variation_thumb"></div>
    <div class="variation_thumb"></div>
</div>

This is what i am getting currently

    <div class="variation_thumb"></div>
    <div class="variation_thumb"></div>
    <div class="variation_thumb"></div>

Please help.

function loop_display_variation_attribute_and_thumbnail() {
        global $product;

        // HERE your targeted product attribute taxonomy
        $taxonomy = 'pa_color';


        if( $product->is_type('variable') ){
            foreach ( $product->get_available_variations() as $variation ){
                if( isset($variation['attributes']['attribute_'.$taxonomy]) ){
          
                 // Get the "pa_color"

                 $term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
    
        
            // Display the product thumbnail
                echo '
                    <div class="'. variation_thumb .'">
                        <img src="' . $variation['image']['thumb_src'] .'">                
                        '. $term_name .'             
                    </div>
                ';
             
                }

            }
        } 
    }

 

✔️Solution:

Echo <div class="all_variations_thumb"> before your foreach loop.

Like so

if( $product->is_type('variable') ){
            echo '<div class="all_variations_thumb">';
            foreach ( $product->get_available_variations() as $variation ){
                if( isset($variation['attributes']['attribute_'.$taxonomy]) ){
          
                 // Get the "pa_color"

                 $term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
    
        
            // Display the product thumbnail
                echo '
                    <div class="'. variation_thumb .'">
                        <img src="' . $variation['image']['thumb_src'] .'">                
                        '. $term_name .'             
                    </div>
                ';
             
                }
             echo '</div>';
            }
        } 

Leave a Reply

Your email address will not be published.

Back to top button