Woocommerce产品变体,Google产品Feed和Wordpress中的嵌套循环

时间:2013-09-10 21:22:31

标签: php wordpress woocommerce google-product-search

我正在尝试从Woocommerce制作Google产品Feed - 可用的插件不支持产品变体,这有点令人讨厌。

我正在开设一个有多种尺寸和颜色的商品的服装店,因此要求Google商品Feed应列出所有可用尺寸/颜色组合,并且因为它正在为每个变体进行库存控制,所以没有必要当我们真正离开的时候将尺寸10白色列为库存尺寸为14号棕色。

所以,我想我需要做的是创建一个产品Feed,为每个产品运行一个循环,然后在该循​​环内,为每个变体运行一个嵌套循环?这有点慢,所以请提出一个更好的方法,如果有的话!至少它只需要每月运行一次。

这是我到目前为止所拥有的:

 <?php
// First get the main product details.  
$args = array( 'post_type' => 'product', 'posts_per_page' => 999 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;

// Now do a second nested loop to get the variations. 
$args2 = array( 'post_type' => 'product_variation', 'post_parent' =>'$id');
$variationloop = new WP_Query( $args2 );
while ( $variationloop->have_posts() ) : $variationloop->the_post();

// get the parent of each variation so we can use $parent (is this necessary???)
$parent = get_post($post->post_parent); 
?>

我觉得我已经完成了第一个循环,我应该可以从中调用它,但是一旦第二个变量循环完成,我似乎无法在第一个循环中引用任何东西,产品环。因此get_post($ post-&gt; post_parent)

然后我做了饲料。到目前为止,这似乎有效:

    <item>
    <title><?php   echo $parent->post_title;?></title>
    <link>http://mysite.com/shop/<?php   echo $parent->post_name;?></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><?php echo $product->is_in_stock() ? get_option('product_in_stock') : get_option('product_out_stock'); ?></g:availability>
    <g:brand>My Brandname</g:brand>
    <g:product_type>Clothing &amp; Accessories &gt;  Clothing &gt;  Swimwear</g:product_type>
    <g:google_product_category>Clothing &amp; Accessories &gt;  Clothing &gt;  Swimwear</g:google_product_category>
    <g:shipping_weight><?php echo $product->get_weight();?></g:shipping_weight>

    <g:mpn><?php echo $product->get_sku(); ?></g:mpn>
    <?php if (get_option('rss_use_excerpt')) : ?>
    <description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>
    <?php else : ?>
    <description><![CDATA[<?php echo $parent->post_excerpt; ?>]]></description>

    <?php endif; ?>

只有这不完整 - 我不确定如何获得我需要的所有元素。特别是,如何检索不同表中的变体大小和颜色?

3 个答案:

答案 0 :(得分:5)

我设法让这个工作,所以留下细节,以防它对任何人都有用。

根据brasofilo的建议,我删除了第二个循环。然后我得到了这样的变化,用meta_compare检查它们是否有库存。

  $args = array( 'post_type' => 'product_variation', 'post_count' =>'9999','meta_key' => '_stock',  'meta_value' => '0', 'meta_compare' => '>');
$variationloop = new WP_Query( $args );
while ( $variationloop->have_posts() ) : $variationloop->the_post();
// get the parent of each variation, so you can refer to things like product description that are set per-product not per-variation. 
$parent = get_post($post->post_parent); 
// is the parent product live? 
if ($parent->post_status=="publish")
{

然后我得到了这样的颜色,尺寸,照片等:

 $colour= get_post_meta($id, 'attribute_pa_colour', true); 
 $size= get_post_meta($id, 'attribute_pa_size', true); 
 $price= get_post_meta($id, '_price', true); 
 $thephoto= get_post_meta($id, '_thumbnail_id', true);  // get photo associated with this variation
 $image_attributes =wp_get_attachment_image_src($thephoto, 'large');

请注意,属性的确切名称将取决于您在设置变体时所谓的属性

然后我只是回应了价值观: POST_TITLE;?&GT; - 尺寸 http://mywebsite.com/shop/ POST_NAME;?&GT; ID; ?&GT;  
post_excerpt); ?&GT;  GBP

 

我希望这对某人有帮助!

答案 1 :(得分:1)

有一个很棒的免费插件可用于支持产品变化的woocommerce产品Feed。 https://wordpress.org/plugins/webappick-product-feed-for-woocommerce/

答案 2 :(得分:0)

由ELEX开发的高级插件甚至支持多种版本。希望这对仍在寻找支持产品变体的WooCommerce的Google产品Feed集成的人们有所帮助。 https://elextensions.com/plugin/woocommerce-google-product-feed-plugin/