Reputation: 560
I am trying to setup a CSV feed for another marketplace. Problem is that only one set of values are stored to the array.
$data = array();
while ($loop->have_posts()) : $loop->the_post();
$product = get_product($loop->post);
$title = $product->get_title();
$link = get_permalink();
$description = strip_tags($post->post_content);
$details = $post->the_excerpt;
$categories = get_the_terms($post->ID, 'product_cat');
$sku = $product->get_sku();
$price = $product->price;
$imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$imageurl = $imageinfo[0];
$image = preg_replace($suchmuster, '', $imageurl);
foreach ($categories as $c) {
$category = $c->name;
}
$data += [
"ean" => $sku,
"condition" => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount" => 9,
"delivery_time" => "b",
"location" => "DE"
];
endwhile;
wp_reset_query();
echo '<pre>';
print_r($data);
echo '</pre>';
My Array now looks like this:
Array
(
[ean] => SportsBag16
[condition] => 100
[listing_price] => 39
[minimum_price] => 39
[amount] => 9
[delivery_time] => b
[location] => DE
)
But there should be way more entries(22).
What am i doing wrong? Thanks for any help.
Upvotes: 0
Views: 22
Reputation: 13083
Your error lies in using +=
to append to the array. Use the following instead:
$data[] = [
"ean" => $sku,
"condition" => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount" => 9,
"delivery_time" => "b",
"location" => "DE"
];
Upvotes: 1
Reputation:
You are append the output to string , you have to make the array in while conditions, in your code it replace the previous value with new values.
$data = array();
while ($loop->have_posts()) : $loop->the_post();
$product = get_product($loop->post);
$title = $product->get_title();
$link = get_permalink();
$description = strip_tags($post->post_content);
$details = $post->the_excerpt;
$categories = get_the_terms($post->ID, 'product_cat');
$sku = $product->get_sku();
$price = $product->price;
$imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$imageurl = $imageinfo[0];
$image = preg_replace($suchmuster, '', $imageurl);
foreach ($categories as $c) {
$category = $c->name;
}
$array1 = array(
"ean" => $sku,
"condition" => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount" => 9,
"delivery_time" => "b",
"location" => "DE"
);
$data []= $array1;
endwhile;
wp_reset_query();
echo '<pre>';
print_r($data);
echo '</pre>';
Upvotes: 1