2

I want to display some variations products by their specified skus and not to display all the variations products within a variable product id.

For example: The Variable product id is: #556

  • Sub variations products of variable product id #556, I want it to display variations products on index-page by their specified skus to display image, title and link to the variable product id #556 page.

My code below is working, but it is showing each and every variations of the products within the variable product id 556. I need to be selective by which, variations products to show by their specified skus to display image, title and link.

Here is my code:

<?php 
$product = new WC_Product_Variable( '556' ); 
$variations = $product->get_available_variations(); 

foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) { 
    $attributes[] = array( 'term_name' => ucwords( str_replace( 'attribute_', '', 
    wc_attribute_taxonomy_slug( $attribute_name ) ) ), 'option' => $attribute, ); 
} 

foreach ( $variations as $variation ) { 
    echo '<div class="index_main_products col-xs-12 col-sm-12 col-md-6 col-lg-4 col-xl-3">';
    echo '<a href="'.$product->get_permalink().'#variations-table">';
    echo "<img src=" . $variation['image']['thumb_src'] .">";
    echo '</a>';

    echo '<h2>';
    echo implode(str_replace('_', ' ',  $variation['attributes']));
    echo '</h2>';

    echo '<a class="index_button button" href="'.$product->get_permalink().'">View Product</a>';
    echo '</div>';
}
?>

Please if you could help. Been trying for soo long and nothing seems to be working.

Thank you in advance.

pv619
  • 409
  • 11
  • 29

1 Answers1

2

You can do this by initializing an array of product variation skus that you don't want to see.

Then in the loop you can check if the current variation sku is present in the array. If yes, do not display it and continue to the next product.

$product = new WC_Product_Variable( 556 );
// initializes an array with product variation skus not to be displayed
$skus = array( 'sku-1', 'sku-2', 'sku-3' );
$variations = $product->get_available_variations(); 

foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) { 
    $attributes[] = array( 'term_name' => ucwords( str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ) ), 'option' => $attribute, ); 
} 

foreach ( $variations as $variation ) { 
    // if the sku of the product variation is not in the array it continues to the next variation
    if ( ! in_array( $variation['sku'], $skus ) ) {
        continue;
    }
    // otherwise
    echo '<div class="index_main_products col-xs-12 col-sm-12 col-md-6 col-lg-4 col-xl-3">';
    echo '<a href="'.$product->get_permalink().'#variations-table">';
    echo "<img src=" . $variation['image']['thumb_src'] .">";
    echo '</a>';
    echo '<h2>';
    echo implode(str_replace('_', ' ',  $variation['attributes']));
    echo '</h2>';
    echo '<a class="index_button button" href="'.$product->get_permalink().'">View Product</a>';
    echo '</div>';
}

The code could not be tested but it should work.

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • Thank you soo much for your help, this works as intended. – pv619 Mar 09 '21 at 09:42
  • Can I please ask one question? How can I get the attributes? For eg: I have attribute set for this variations product and want to display attribute by 'slug-name' which is 'voltage'. Is it possible to do so? Thank you once again for your great help. Have a great day. – pv619 Mar 09 '21 at 09:45
  • 1
    With `$variation['attributes']` you get an array with all the attributes of the product variation. If you want to get only the attribute values with slug **voltage** you can use `$variation['attributes']['attribute_pa_voltage']`. This way you get the slug (or slugs) of the variation attribute. To get the name from the attribute term slug look [here](https://stackoverflow.com/questions/35130013/how-to-get-attribute-name-instead-of-slug-in-variation). – Vincenzo Di Gaetano Mar 09 '21 at 19:12
  • Thank you soo much for the information. This resolved my issue and have a great day :) – pv619 Mar 14 '21 at 16:11