I'm trying to populate an empty array with the name and value from a foreach loop. I've created an array $variationimages = array(); and within the loop I'm saving the values by using $variationimages['name'] and $variationimages['image'].
When returning the $variationimages array I'm only getting the first item
Array
(
[name] = Wall Profile
[image] = <img src="example-image.jpg">
)
What I would like is for it to return something like the below and to be able to access a specific item in the array by using something like $variationimages['Roof Profile']['image'] which would return the image of the item with the name 'Roof Profile'.
Array
(
[name] = Wall Profile
[image] = <img src="example-image.jpg">
),
(
[name] = Roof Profile
[image] = <img src="example-image.jpg">
),
The code I have is below:
function loop_display_variation_attribute_and_thumbnail() {
global $product;
// HERE your targeted product attribute taxonomy
$taxonomy = 'pa_product_type';
$variationimages = array();
if( $product->is_type('variable') ) {
foreach ( $product->get_available_variations() as $variation ) {
if( isset($variation['attributes']['attribute_'.$taxonomy]) ) {
// Get the "pa_product_type"
$term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
$term_slug = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->slug;
$variationimages['name'] = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
$variationimages['image'] = '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">';
}
}
echo "<div style='display:none'>";
print_r($variationimages);
echo "</div>";
}
}