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.
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.
The code could not be tested but it should work.