I try to output a simple array in php to use it for my google shopping data feed(XML) but unfortunatly its not that simple…
This is the array I want to output:
$description = $post->post_content;
It’s the Product description of a Woocommerce product in my Shop.
All Details like pricing and short description come in a string except the product description. It’s an array with single letters in it. I can output them like:
$description[0] = (first letter in description)
$description[1] = (next letter... and so on...)
So my question is: How can I echo them out at once?
here’s my code:
<?php
require_once("../../../wp-load.php");
// Set the xml header
header('Content-Type: text/xml; charset=UTF-8');
// Echo out all the details
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>M13 Shop Feed</title>
<link>http://www.manufaktur13.de</link>
<description>Google Merchant Feed</description>';
$args = array(
'post_type' => 'product',
'product_cat' => 'tabak-taschen , accessoires',
'orderby' => '_sku'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = get_product($loop->post);
$title = $product->get_title();
$link = get_permalink();
$description = $post->post_content; // <--this is the strange array
$test = implode(' ', $description ); // <--testing implode, not working :(
$sku = $product->get_sku();
$price = $product->price;
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
echo "<item>
<title>$title</title>
<link>$link</link>
// Testing the variables in description to see if they are correct
<description>$price â¬, $sku , $image[0] , $description[0]$description[1]$description[2]$description[3]$description[4]$description[5] $test</description>
<g:google_product_category>Heim & Garten > Rauchzubehör</g:google_product_category>
<g:id>$sku</g:id>
<g:condition>Neu</g:condition>
<g:price>$price â¬</g:price>
<g:availability>Auf Lager</g:availability>
<g:image_link>$image[0]</g:image_link>
<g:shipping>
<g:country>DE</g:country>
<g:service>Standard</g:service>
<g:price>0.00 â¬</g:price>
</g:shipping>
<g:gtin></g:gtin>
<g:brand>Manufaktur13</g:brand>
<g:mpn></g:mpn>
<g:product_type>Tabaktasche</g:product_type>
<g:identifier_exists>false</g:identifier_exists>
</item>";
endwhile;
wp_reset_query();
echo "</channel>
</rss>";
?>
LIVE-EXAMPLE: XML Google Shopping Data Feed
Last question: Why is there an array full of single letters?
The reason your implode application isn’t working is because $description is an associative array. I would recommend making it a multidimensional array so you could implode. You would do so with this code: