Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Last active November 14, 2019 10:42
Show Gist options
  • Select an option

  • Save pavlo-bondarchuk/2ba78240ab9afcdfab5640aeab2f3a9e to your computer and use it in GitHub Desktop.

Select an option

Save pavlo-bondarchuk/2ba78240ab9afcdfab5640aeab2f3a9e to your computer and use it in GitHub Desktop.
quick generate google_shop_feed.xml on wordpress
<?php
require_once( dirname( __FILE__ ) . '/wp-load.php' );
$args = array(
'post_type' => 'product',
'posts_per_page' => 9999999
);
$query = new \WP_Query($args);
$getCategories = get_categories( array(
'taxonomy' => 'product_cat',
));
if ( $query->have_posts() ) {
$loopCount = 0;
$file = $_SERVER['DOCUMENT_ROOT'] . '/google_shop_feed.xml';
$optionFirmId = get_option( 'my_option_xml_firmId' );
$optionRate = get_option( 'kurs' );
$headerXml = '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>test</title>
<link>https://starterkit.com.ua</link>
<description>Максимальный комфорт вашего жилья благодаря системам умного дома</description>';
$footerXml = '</rss>';
$current .= $headerXml;
while ($query->have_posts()) {
$query->the_post();
global $post;
// global $product;
// global $woocommerce;
$custom_text = get_post_custom_values('text_for_xml');
$custom_vendor = get_post_custom_values('factory_xml');
$custom_vendor = ($custom_vendor[0]) ? $custom_vendor[0] : 'Ajax';
$custom_code = get_post_custom_values('code_from_shop');
$category = get_the_terms( $post->ID, 'product_cat' );
$price = $product->get_price();
$stock = get_post_meta( get_the_ID(), '_stock_status', true);
$priceArr = explode('-', $price);
$current .= '<item>
<g:id>'. $product->get_id() .'</g:id>
<g:title>'. $product->get_name() .'</g:title>
<g:description>'. $custom_text[0] .'</g:description>
<g:link>'. get_the_permalink() .'</g:link>
<g:image_link>'. get_the_post_thumbnail_url() .'</g:image_link>';
if( $stock == instock ){
$current .= '<g:availability>in stock</g:availability>';
} else {
$current .= '<g:availability>preorder</g:availability>';
};
$current .= '<g:price>'. round($price, 2) .' UAH</g:price>
<g:brand>'. $custom_vendor .'</g:brand>';
$sep = ' > ';
$cat_name = $sep . $category[1]->name;
$current .= '<g:product_type>'. $category[0]->name .' '. $cat_name .'</g:product_type>';
$current .= '</item>';
$loopCount++;
}
$current .= '</channel>';
$current .= $footerXml;
$res = file_put_contents($file, $current);
echo 'Post update! Count = ' . $loopCount;
echo '<br>';
echo current_time('d/m/Y H:i');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment