示例#1
0
文件: index.php 项目: phn007/MyTools
?>
	<section class="main-product">
		<div class="image"><?php 
image($image_url, $keyword, $goto);
?>
</div>
		<div class="info">
			<h1><?php 
title($goto, $keyword);
?>
</h1>
			<?php 
social($permalink, $keyword);
?>
			<div class="brand"><?php 
brand($brandLink, $brand);
?>
</div>
			<hr>
			<div class="price">$<?php 
echo $price;
?>
</div>
			<div class="button">
				<span id="butt">
					<?php 
button($goto, $keyword);
?>
				</span> 
			</div>
			<hr>
function generateGoogle()
{
    date_default_timezone_set("America/Denver");
    //include( plugin_dir_path( __FILE__ ) . 'functions.php');
    global $woocommerce, $product, $post, $re_wcvt_options, $xml;
    $args = array('post_type' => 'product', 'posts_per_page' => -1);
    $loop = new WP_Query($args);
    $xml = new DOMDocument('1.0', 'UTF-8');
    $xml->formatOutput = true;
    $rss = $xml->createElement("rss");
    $rss_node = $xml->appendChild($rss);
    //add RSS element to XML node
    $rss_node->setAttribute("version", "2.0");
    //set RSS version
    $rss_node->setAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
    $rss_node->setAttribute("xmlns:g", "http://base.google.com/ns/1.0");
    $currentItem = $xml->createElement("channel");
    $currentItem = $rss_node->appendChild($currentItem);
    $currentItem->appendChild($xml->createElement('title', 'Novosbed'));
    $buildLinks = $xml->createElement('link');
    $buildLinks = $currentItem->appendChild($buildLinks);
    $buildLinks->appendChild($xml->createCDATASection('http://wwww.novosbed.com'));
    $currentItem->appendChild($xml->createElement('description', '----------------------------------------------'));
    while ($loop->have_posts()) {
        $loop->the_post();
        global $product;
        $product = get_product($loop->post);
        $attributes = $product->get_attributes();
        //print_r($product); // Debug option make sure is commented out live
        //print_r($attributes);
        //checks if product is in stock and visable to public.
        if ($product->is_in_stock() && $product->is_visible()) {
            /* append it to the document created */
            // Checks if product has sizes (twin/twin XL.. etc)
            if ($product->has_child()) {
                // Loops through products and gets variation attriubtes (sku/weight/etc)
                $ProdVar = $product->get_available_variations();
                foreach ($ProdVar as $Variation) {
                    $buildItem = $xml->createElement("item");
                    $buildItem = $currentItem->appendChild($buildItem);
                    $buildItem->appendChild($xml->createElement('g:title', ucfirst(get_the_title()) . ' ' . ucfirst($Variation['attributes']['attribute_pa_firmness'])));
                    // This is not working yet
                    $buildLink = $xml->createElement('g:link');
                    $buildLink = $buildItem->appendChild($buildLink);
                    $buildLink->appendChild($xml->createCDATASection(ucfirst(get_permalink())));
                    $buildItem->appendChild($xml->createElement('g:type', ucfirst(getGType())));
                    $buildCat = $xml->createElement('g:google_product_catergory');
                    $buildCat = $buildItem->appendChild($buildCat);
                    $buildCat->appendChild($xml->createCDATASection(getCat()));
                    $buildItem->appendChild($xml->createElement('g:brand', brand()));
                    $buildItem->appendChild($xml->createElement('g:availablility', 'In Stock'));
                    $buildItem->appendChild($xml->createElement('g:condition', 'New'));
                    //Add shipping branch
                    $buildShipping = $xml->createElement("g:shipping");
                    $buildShipping = $buildItem->appendChild($buildShipping);
                    $buildShipping->appendChild($xml->createElement('g:country', 'US'));
                    $buildShipping->appendChild($xml->createElement('g:service', 'FedEx Ground'));
                    $buildShipping->appendChild($xml->createElement('g:price', '0.00 USD'));
                    //Back to Item
                    $buildItem->appendChild($xml->createElement('g:ID', $Variation['sku']));
                    // loop multi dimensional array to get size
                    $buildItem->appendChild($xml->createElement('g:size', ucfirst($Variation['attributes']['attribute_pa_size'])));
                    $buildItem->appendChild($xml->createElement('g:price', '$' . strip_tags($Variation['display_price'] . ' USD')));
                    // Strips 'lbs' from weight retrived even if empty variable
                    $weight = str_replace(array(' ', 'l', 'b', 's'), '', $Variation['weight']);
                    if (!empty($weight)) {
                        // if number remains print weight + lbs
                        $buildItem->appendChild($xml->createElement('g:weight', $weight . ' lbs'));
                    }
                    if (!empty($Variation['dimensions'])) {
                        // if product has data in dimensions will echo
                        $buildItem->appendChild($xml->createElement('g:dimensions', $Variation['dimensions']));
                    }
                    $buildDesc = $xml->createElement('g:description');
                    $buildDesc = $buildItem->appendChild($buildDesc);
                    $buildDesc->appendChild($xml->createCDATASection(strip_tags($Variation['variation_description'])));
                    //print_r($Variation); // Debug option make sure is commented out live
                    // loops through gallry image items and retrives file path.
                    if (metadata_exists('post', $post->ID, '_product_feed_gallery')) {
                        $product_image_gallery = get_post_meta($post->ID, '_product_feed_gallery', true);
                        $attachments = array_filter(explode(',', $product_image_gallery));
                        if ($attachments) {
                            $i = 0;
                            foreach ($attachments as $attachment_id) {
                                $image_link = wp_get_attachment_url($attachment_id);
                                if ($i < 1) {
                                    $buildImage = $xml->createElement('g:image');
                                    $buildImage = $buildItem->appendChild($buildImage);
                                    $buildImage->appendChild($xml->createCDATASection($image_link));
                                    $i++;
                                } else {
                                    $buildAddImage = $xml->createElement('g:additional_image');
                                    $buildAddImage = $buildItem->appendChild($buildAddImage);
                                    $buildAddImage->appendChild($xml->createCDATASection($image_link));
                                    $i++;
                                }
                            }
                        }
                    }
                    // end foreach// end foreach
                    $buildItem->appendChild($xml->createElement('g:mpn', $Variation['sku']));
                }
            }
            //echo '<br>';
        }
        // Loop trough rest of products available that have no sizes.
        //checks for products with no child objects and is visable
        if (!$product->has_child() && $product->is_visible()) {
            $buildItem = $xml->createElement("item");
            $buildItem = $currentItem->appendChild($buildItem);
            $buildItem->appendChild($xml->createElement('g:title', get_the_title()));
            // This is not working yet
            $buildLink = $xml->createElement('g:link');
            $buildLink = $buildItem->appendChild($buildLink);
            $buildLink->appendChild($xml->createCDATASection(ucfirst(get_permalink())));
            $buildItem->appendChild($xml->createElement('g:type', getGType()));
            $buildCat = $xml->createElement('g:google_product_catergory');
            $buildCat = $buildItem->appendChild($buildCat);
            $buildCat->appendChild($xml->createCDATASection(getCat()));
            $buildItem->appendChild($xml->createElement('g:brand', brand()));
            $buildItem->appendChild($xml->createElement('g:availablility', 'In Stock'));
            $buildItem->appendChild($xml->createElement('g:condition', 'New'));
            //Add shipping branch
            $buildShipping = $xml->createElement("g:shipping");
            $buildShipping = $buildItem->appendChild($buildShipping);
            $buildShipping->appendChild($xml->createElement('g:country', 'US'));
            $buildShipping->appendChild($xml->createElement('g:service', 'FedEx Ground'));
            $buildShipping->appendChild($xml->createElement('g:price', '0.00 USD'));
            //Back to Item
            $buildItem->appendChild($xml->createElement('g:ID', $product->sku));
            //$buildItem->appendChild($xml->createElement('g:size', ucfirst($Variation['attributes']['attribute_pa_size'])));
            $buildItem->appendChild($xml->createElement('g:price', '$' . $product->price . ' USD'));
            // Strips 'lbs' from weight retrived even if empty variable
            $weight = str_replace(array(' ', 'l', 'b', 's'), '', $product->weight);
            if (!empty($weight)) {
                // if number remains print weight + lbs
                $buildItem->appendChild($xml->createElement('g:weight', $product->weight . ' lbs'));
            }
            if (!empty($product->length)) {
                // if product has data in dimensions will echo
                $buildItem->appendChild($xml->createElement('g:dimensions', $product->length . '"x' . $product->width . '"x' . $product->height . '"'));
            }
            $buildDesc = $xml->createElement('g:description');
            $buildDesc = $buildItem->appendChild($buildDesc);
            $buildDesc->appendChild($xml->createCDATASection(strip_tags(description($post))));
            if (metadata_exists('post', $post->ID, '_product_feed_gallery')) {
                $product_image_gallery = get_post_meta($post->ID, '_product_feed_gallery', true);
                $attachments = array_filter(explode(',', $product_image_gallery));
                if ($attachments) {
                    $i = 0;
                    foreach ($attachments as $attachment_id) {
                        $image_link = wp_get_attachment_url($attachment_id);
                        if ($i < 1) {
                            $buildImage = $xml->createElement('g:additional_image');
                            $buildImage = $buildItem->appendChild($buildImage);
                            $buildImage->appendChild($xml->createCDATASection($image_link));
                            $i++;
                        } else {
                            $buildAddImage = $xml->createElement('g:additional_image');
                            $buildAddImage = $buildItem->appendChild($buildAddImage);
                            $buildAddImage->appendChild($xml->createCDATASection($image_link));
                            $i++;
                        }
                    }
                }
            }
            // end foreach// end foreach
            $buildItem->appendChild($xml->createElement('g:mpn', $product->sku));
        }
    }
    echo writeFile($xml);
    wp_reset_query();
}