Last active
November 16, 2016 22:56
-
-
Save sirtimid/f77cc8ad2405c0ee8ad50819c3cb5946 to your computer and use it in GitHub Desktop.
Wordpress WooCommerce Skroutz 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 | |
/* | |
*Template Name: Skroutz XML | |
*/ | |
/* | |
* This Wordpress template generates a xml feed for skroutz.gr | |
*/ | |
header ("Content-Type:text/xml"); | |
$posts = get_posts(array( | |
'numberposts' => -1, | |
'post_type' => 'product', | |
'post_status' => 'publish' | |
)); | |
$XML = '<?xml version="1.0" encoding="utf-8"?>'; | |
$XML .= "\n\t".'<webstoresname url="'.home_url().'" encoding="utf8">'; | |
$XML .= "\n\t\t".'<created_at>'.date("Y-m-d H:i:s").'</created_at>'; | |
$XML .= "\n\t\t".'<products>'; | |
foreach ( $posts as $post ) { | |
setup_postdata($post); | |
$product = wc_get_product($post->ID); | |
// get date | |
$postdate = explode(' ', $post->post_date); | |
// get image | |
$image = wp_get_attachment_url($post->ID); | |
// get price | |
$price = $product->get_price(); | |
// get brands | |
$brands = wp_get_post_terms($post->ID, 'product_brand', array('fields' => 'names', 'orderby' => 'parent', 'order' => 'ASC')); | |
$brand = implode(' > ', array_filter($brands)); | |
// get categories | |
$categories = wp_get_post_terms($post->ID, 'product_cat', array('fields' => 'names', 'orderby' => 'parent', 'order' => 'ASC')); | |
$category = implode(' > ', array_filter($categories)); | |
$XML .= "\n\t\t\t".'<product id="'. $post->ID .'">'; | |
$XML .= "\n\t\t\t\t".'<id>'. $post->ID .'</id>'; | |
$XML .= "\n\t\t\t\t".'<name><![CDATA['. get_the_title() .']]></name>'; | |
$XML .= "\n\t\t\t\t".'<link><![CDATA['. urldecode(get_permalink()) .']]></link>'; | |
$XML .= "\n\t\t\t\t".'<image><![CDATA[http:'. urldecode($image) .']]></image>'; | |
$XML .= "\n\t\t\t\t".'<description><![CDATA['. strip_tags(apply_filters('the_content', $post->post_content)) .']]></description>'; | |
$XML .= "\n\t\t\t\t".'<price_with_vat>'.number_format((float)$price, 2, '.', '').'</price_with_vat>'; | |
$XML .= "\n\t\t\t\t".'<category><![CDATA['.$category.']]></category>'; | |
$XML .= "\n\t\t\t\t".'<manufacturer><![CDATA['.$brand.']]></manufacturer>'; | |
if(!$product->is_in_stock()){ | |
$XML .= "\n\t\t\t\t".'<instock>N</instock>'; | |
$XML .= "\n\t\t\t\t".'<availability>Κατόπιν Παραγγελίας</availability>'; | |
}else{ | |
$XML .= "\n\t\t\t\t".'<stock>Y</stock>'; | |
$XML .= "\n\t\t\t\t".'<availability>Άμεσα διαθέσιμο</availability>'; | |
} | |
$XML .= "\n\t\t\t".'</product>'; | |
} | |
$XML .= "\n\t\t".'</products>'; | |
$XML .= "\n\t".'</webstoresname>'; | |
echo $XML; | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment