65535 65535 - 6 months ago 21
PHP Question

WooCommerce: How Can A Product Weight Be Returned Using PHP?

I am setting up a Google Merchant Feed, and one of the requirements is to include a shipping weight of the product. I am using this tutorial: http://www.binaryforest.com/woocommerce-google-product-feed/

How can the product weight be returned by php into the xml?

Any help would be greatly appreciated.

functions.php:

//Define the product feed php page
function products_feed_rss2() {
$rss_template = get_template_directory() . '/product-feed.php';
load_template ( $rss_template );
}

//Add the product feed RSS
add_action('do_feed_products', 'products_feed_rss2', 10, 1);

//Update the Rerewrite rules
add_action('init', 'my_add_product_feed');

//function to add the rewrite rules
function my_rewrite_product_rules( $wp_rewrite ) {
$new_rules = array(
'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

//add the rewrite rule
function my_add_product_feed( ) {
global $wp_rewrite;
add_action('generate_rewrite_rules', 'my_rewrite_product_rules');
$wp_rewrite->flush_rules();
}


product-feed.php

<?php
/**
* RSS2 Feed Template for displaying RSS2 Posts feed.
*
* @package WordPress
*/

header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
$more = 1;

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:g="http://base.google.com/ns/1.0"
<?php do_action('rss2_ns'); ?>
>

<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description>Skin care products for rosacea sufferers</description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 999 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<item>
<title><?php the_title_rss() ?></title>
<link><?php the_permalink_rss() ?></link>
<g:image_link><?php echo wp_get_attachment_url( get_post_thumbnail_id() ) ?></g:image_link>
<g:price><?php echo $product->price ?></g:price>
<g:condition>new</g:condition>
<g:id><?php echo $id; ?></g:id>
<g:availability>in stock</g:availability>
<g:google_product_category>Health &amp; Beauty &gt; Health Care &gt; Fitness &amp; Nutrition &gt; Vitamins &amp; Supplements </g:google_product_category>
<g:gtin><?php echo get_post_meta( get_the_ID(), '_cpf_upc', true ); ?></g:gtin>
<g:brand><?php echo get_post_meta( get_the_ID(), '_cpf_brand', true ); ?></g:brand>
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php endif; ?>

<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>

Answer

This is the working answer:

<g:shipping_weight><?php echo $product->weight ?></g:shipping_weight>
Comments