Bill Bronson Bill Bronson - 3 days ago 4
PHP Question

WooCommerce post description does not display in XML output

I want to make a dynamicly generated XML for Google Shopping from our Woocommerce shop. So long so good... ...everything is working correctly but I can not output the product description at all.

I tried:

get_the_content();
the_content();
$post->post_content


Here's my code:

<?php
require_once("../../../wp-load.php");

// Set the xml header
header("Content-type: text/xml");

// Echo out all the details
echo '<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>M13 Shop Feed</title>
<link>----</link>
<description>Google Merchant Feed</description>';

$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => 'tabak-taschen',
'orderby' => '_sku',
);

query_posts($args);
if (have_posts()):

while (have_posts()) : the_post();



$brand = '----';
$title = $product->get_title();
$link = get_permalink();
$description = $post->post_content;
$sku = $product->get_sku();
$price = $product->price;
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID)); //getting image source


echo "<item>
<title>$title</title>
<link>$link</link>
<description>$price €, $sku , $image[0] , $description</description>
<g:google_product_category>Heim &amp; Garten &gt; Rauchzubehör</g:google_product_category>
<g:id>$sku</g:id>
<g:condition>Neu OVP</g:condition>
<g:price>$price €</g:price>
<g:availability>Auf Lager</g:availability>
<g:image_link>$image</g:image_link>
<g:shipping>
<g:country>UK</g:country>
<g:service>Standard</g:service>
<g:price>5.95 GBP</g:price>
</g:shipping>
<g:gtin></g:gtin>
<g:brand>----</g:brand>
<g:mpn></g:mpn>
<g:product_type>Tabaktasche</g:product_type>
</item>";

endwhile;

endif;
?>


LIVE-EXAMPLE: XML Google Data Feed

I am wondering why this happens because
the_excerpt();
and other functions work totally normal except
the_content()
or
$post->post_content;
.

For better understanding I put some of the variables into the description to proof that they are working.

Answer

Some issues:

  • $product is not defined anywhere.
  • personally, I prefer get_posts() than query_posts().
  • in <g:image_link>, you should use $image[0].
  • you're not closing <rss> and <channel>.

The following works:

<?php
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );

// Set the xml header
header("Content-type: text/xml");

// Echo out all the details
echo '<?xml version="1.0"?> 
<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',
    'posts_per_page' => -1
);
$posts = get_posts($args);
if ( $posts ):
    foreach( $posts as $post ) 
    {
        $brand = 'Manufaktur13';
        $title = $post->post_title;
        $link = get_permalink($post->ID);
        $description = $post->post_content;
        $product = get_product( $post );
        $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>
        <description>$price €, $sku , $image[0] , $description</description>
        <g:google_product_category>Heim &amp; Garten &gt; Rauchzubehör</g:google_product_category>
        <g:id>$sku</g:id>
        <g:condition>Neu OVP</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>UK</g:country>
            <g:service>Standard</g:service>
            <g:price>5.95 GBP</g:price>
        </g:shipping>
        <g:gtin></g:gtin>
        <g:brand>Manufaktur13</g:brand>
        <g:mpn></g:mpn>
        <g:product_type>Tabaktasche</g:product_type>
    </item>
            ";
    }
endif;

echo '
</channel></rss>';
Comments