aderaaij
2/25/2014 - 9:57 PM

Custom feed used to import WooCommerce product variations into other stores, etc. In this particular example each WooCommerce variation is q

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>