Skip to content

Instantly share code, notes, and snippets.

@merqlove
Last active September 17, 2015 08:02
Show Gist options
  • Save merqlove/f0112e88d46b14981d74 to your computer and use it in GitHub Desktop.
Save merqlove/f0112e88d46b14981d74 to your computer and use it in GitHub Desktop.
WooCommerce - Yandex Market XML Example
<?php
/*
Template name: Для яндекс маркета
*/
header('Content-Type: text/xml; charset=utf-8');
preg_match('|http://(.*)|', get_option('siteurl'), $m);
$shop_name = $m[1];
echo '<?xml version="1.0" encoding="utf-8" ?>'."\n".
'<!DOCTYPE yml_catalog SYSTEM "shops.dtd">'."\n\n".
"<yml_catalog date=\"".date('Y-m-d H:i')."\">\n".
"<shop>\n".
"<name>".$shop_name."</name>\n".
"<company>".get_option('blogname')."</company>\n".
"<url>".get_option('siteurl')."</url>\n\n".
"<currencies>\n".
"\t".'<currency id="RUR" rate="1"/>'."\n".
"</currencies>\n\n".
"<categories>\n";
// $args = array(
// 'number' => $number,
// 'orderby' => $orderby,
// 'order' => $order,
// 'hide_empty' => $hide_empty,
// 'include' => $ids
// );
$args = array(
'hide_empty' => true
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
print "\t".'<category id="'.$product_category->term_taxonomy_id.'">'.$product_category->name.'</category>'."\n";
}
}
print
"</categories>\n\n".
"<offers>\n";
$args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$picture = wp_get_attachment_url( $product->get_image_id() );
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$title = get_the_title();
$content = get_the_content();
$active = $product->is_purchasable() ? 'true' : 'false';
$permalink = get_permalink();
echo "<offer id='{$product->id}' available='true'>\n";
echo "\t<url>{$permalink}</url>\n";
echo "\t<price>{$price}</price>\n";
echo "\t<currencyId>RUR</currencyId>\n";
echo "\t<picture>{$picture}</picture>\n";
echo "\t<delivery>{$active}</delivery>\n";
echo "\t<name>{$title}</name>\n";
echo "\t<description>{$content}</description>\n";
// //echo "\t<sales_notes>{$time}</sales_notes>\n";
echo "</offer>\n";
endwhile;
wp_reset_query();
echo "</offers>\n\n";
echo "</shop>\n";
echo "</yml_catalog>\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment