I did create a function which loops through the colors of a product and outputs a div with the specific color. After that it finds the image for the available colors. But I think the code can be a lot smarter.
Can someone optimize my code?
<?php
$brand_terms = get_the_terms($post, 'pa_kleur');
$brand_string = ''; // Reset string
foreach ($brand_terms as $term) : ?> <div style="display: block; margin-bottom: 50px;"><?php
if (($term->name) == 'Roze') {
echo '<div style="width: 20px; height: 20px; background-color: pink;" class="roze-kleur"></div>';
$variations = $product->get_available_variations();
foreach ( $variations as $key => $value ) {
?>
<?php if ($value['attributes']['attribute_pa_kleur'] == 'roze') { ?>
<li>
<span><?php echo $value['image']['url']; }?></span>
</li></div>
<?php
}
}
if (($term->name) == 'Grijs') {
echo '<div style="width: 20px; height: 20px; background-color: grey;" class="grijze-kleur"></div>';
$variations = $product->get_available_variations();
foreach ( $variations as $key => $value ) {
?>
<?php if ($value['attributes']['attribute_pa_kleur'] == 'grijs') { ?>
<li>
<span><?php echo $value['image']['url']; }?></span>
</li></div>
<?php
}
}
endforeach;
?>
Thanks for your time!