0

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.

2 Answers 2

1

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>';
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my gosh i know it would be that simple. :) Thanks mate!
1

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"
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.