Created
March 22, 2017 20:44
-
-
Save marcosnakamine/c6126cbcd749a6d91eeddca8ea2d75ff to your computer and use it in GitHub Desktop.
WooCommerce - Google Merchant xml feed generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php header( 'Content-type: text/xml' ) ?> | |
<?php echo '<?xml version="1.0"?>' ?> | |
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> | |
<channel> | |
<title><?php echo bloginfo( 'name' ) ?></title> | |
<link><?php echo home_url() ?></link> | |
<description><?php echo bloginfo( 'description' ) ?></description> | |
<?php | |
$query = new WP_Query( array( | |
'post_type' => 'product', | |
'posts_per_page' => -1, | |
'post_status' => 'publish' | |
) ); | |
?> | |
<?php while ($query->have_posts()) : $query->the_post(); ?> | |
<?php $product = wc_get_product( get_the_ID() ); ?> | |
<?php if ( $product->get_type() == 'simple' ) : ?> | |
<?php if ( $product->is_in_stock() ) : ?> | |
<?php $price = $product->get_sale_price() == '' ? $product->get_regular_price() : $product->get_sale_price() ?> | |
<item> | |
<g:id><?php echo $product->get_sku() ?></g:id> | |
<g:title><?php the_title() ?></g:title> | |
<g:description><?php echo strip_tags( get_the_excerpt() ) ?></g:description> | |
<g:link><?php the_permalink() ?></g:link> | |
<g:image_link><?php $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) ); echo $img[0]; ?></g:image_link> | |
<g:condition>new</g:condition> | |
<g:availability>in stock</g:availability> | |
<g:price><?php echo $product->get_sale_price() ?></g:price> | |
<g:brand><?php echo bloginfo( 'name' ) ?></g:brand> | |
<g:mpn>FALSE</g:mpn> | |
</item> | |
<?php endif ?> | |
<?php else : ?> | |
<?php $product_variation = $product->get_children() ?> | |
<?php foreach ($product_variation as $key => $value): ?> | |
<?php if ( $product->is_in_stock() ) : ?> | |
<?php $product = $product->get_child( $value ) ?> | |
<?php $price = $product->get_sale_price() == '' ? $product->get_regular_price() : $product->get_sale_price() ?> | |
<item> | |
<g:id><?php echo $product->get_sku() ?></g:id> | |
<g:title><?php the_title() ?></g:title> | |
<g:description><?php echo strip_tags( get_the_excerpt() ) ?></g:description> | |
<g:link><?php the_permalink() ?></g:link> | |
<g:image_link><?php $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) ); echo $img[0]; ?></g:image_link> | |
<g:condition>new</g:condition> | |
<g:availability>in stock</g:availability> | |
<g:price><?php echo $price ?></g:price> | |
<g:brand><?php echo bloginfo( 'name' ) ?></g:brand> | |
<g:mpn>FALSE</g:mpn> | |
</item> | |
<?php endif ?> | |
<?php endforeach ?> | |
<?php endif ?> | |
<?php endwhile ?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment