Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Last active June 29, 2020 15:34
Show Gist options
  • Save aderaaij/9218744 to your computer and use it in GitHub Desktop.
Save aderaaij/9218744 to your computer and use it in GitHub Desktop.
Custom feed used to import WooCommerce product variations into other stores, etc. In this particular example each WooCommerce variation is queried as a single product).
<?php
/**
* Template Name: custom woocommerce feed
* Description: Custom feed for importing woocommerce products in other stores
* Author: Arden de Raaij
*
* @package WordPress
*
*
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
// set global $product to fetch all woocommerce product data
global $product, $woocommerce_loop;
// query arguments
$args =
array(
'post_type' => 'product',
'posts_per_page' => -1
);
//query the posts
$products = new WP_Query( $args );
//define header and echo xml version
header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?>
<customfeed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Settings>
<disablemissingproducts>true</disablemissingproducts>
<updatechanges>false</updatechanges>
<updatefrequency>15</updatefrequency>
</Settings>
<Products>
<?php
//begin looping products
while ( $products->have_posts() ) : $products->the_post();
// declare global post variables
$productID = get_the_ID();
// declare new excerpt variable
$myExcerpt = get_the_excerpt();
$tags = array("<p>", "</p>");
$myExcerpt = str_replace($tags, "", $myExcerpt);
// create a loop of all variations
$available_variations = $product->get_available_variations();
// begin looping variations
foreach ($available_variations as $prod_variation) :
// get post object variable
$post_id = $prod_variation['variation_id'];
$post_object = get_post($post_id);
// product variation variables
$price = get_post_meta( $post_object->ID, '_regular_price', true);
$sales_price = get_post_meta( $post_object->ID, '_sales_price', true);
$sales_from_date = ( $date = get_post_meta( $post_object->ID, '_sale_price_dates_from', true ) ) ? date_i18n( 'm-d-Y', $date ) : '';
$sales_to_date = ( $date = get_post_meta( $post_object->ID, '_sale_price_dates_to', true ) ) ? date_i18n( 'm-d-Y', $date ) : '';
$stock = get_post_meta( $post_object->ID, '_stock', true);
$size = get_post_meta( $post_object->ID, 'attribute_pa_size', true);
// get images
if ( has_post_thumbnail()):
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
endif;
if(get_field('second_thumbnail')):
$attachment_id = get_field('second_thumbnail');
$size = "large"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
endif;
?>
<product>
<sku><?php echo $productID.'-'.$post_id.'-'.$size;?></sku>
<name><?php the_title();?></name>
<price><?php echo $price;?></price>
<taxclass>high</taxclass>
<stockqty><?php echo $stock;?></stockqty>
<productgroupid><?php echo $productID;?></productgroupid>
<configureby>size</configureby>
<?php if(get_the_content()):?>
<description><?php the_content();?></description>
<?php endif?>
<shortdescription><?php echo $myExcerpt;?></shortdescription>
<metatitle><?php the_title();?></metatitle>
<metakeywords>Example metakeywords</metakeywords>
<metadescription>Example metadescription</metadescription>
<brand><?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?></brand>
<size><?php echo $size; ?></size>
<images>
<imageurl><?php echo $large_image_url[0]; ?></imageurl>
<?php $attachment_ids = $product->get_gallery_attachment_ids();
$loop = 0;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
foreach ( $attachment_ids as $id ):
$gallery_image = wp_get_attachment_image_src( $id, 'full');
?>
<imageurl> <?php echo $gallery_image[0];?></imageurl>
<?php $loop++;
endforeach;
?>
</images>
<?php if($sales_price):?>
<specialprice xsi:nil="true"><?php echo $sales_price; ?></specialprice> <!-- enduser offer price -->
<?php endif;?>
<?php if($sales_from_date):?>
<specialpricefromdate xsi:nil="true"><?php echo $sales_from_date;?></specialpricefromdate> <!-- enduser offer price visible from date us date format (mm-dd-yyyy) e.g. 12/31/2013 -->
<?php endif;?>
<?php if($sales_to_date):?>
<specialpricetodate xsi:nil="true"><?php echo $sales_to_date;?></specialpricetodate> <!-- enduser offer price visible to date us date format (mm-dd-yyyy) e.g. 12/31/2013 -->
<?php endif;?>
</product>
<?php
endforeach;
endwhile;
?>
</products>
</customfeed>
@umarglobal
Copy link

Hi

Thank you for this. It seems like you have used this as a template page. Would I then assign this to a page in wordpress? How would I export the XML file?

@aderaaij
Copy link
Author

Hi

Thank you for this. It seems like you have used this as a template page. Would I then assign this to a page in wordpress? How would I export the XML file?

That's right indeed. I didn't export it myself but let someone else import it from the WP page I published. To be honest, I can't remember if it ever worked properly..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment