Reputation: 811
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>";
}
}
Upvotes: 0
Views: 37
Reputation: 1
i think you must fill array with objects or array of arrays
like , you must create class for example Item $one_item= new Item();
and write code like that:
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;
$one_item=New Item ();
$one_item=>name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
$one_item=>image = '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">';
array_push($variationimages,$one_item);
}
}
echo "<div style='display:none'>";
print_r($variationimages);
echo "</div>";
}
}
Upvotes: 0
Reputation: 178
Shaun, just add additional [] on your variable here:
$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'] .'">';
On every iteration it will append new values.
Cheers ;-)
---EDIT after comment---
$variationimages[] = [
'name' => get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name,
'image' => '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">'
];
Upvotes: 1