我正试图从Woocommerce安装中拉出一个产品变体列表,并列出它们的属性-如下所示:
product id variationid name color size
5 1234 swimsuit blue 10
5 1235 swimsuit blue 12
5 1236 swimsuit blue 14
5 1237 swimsuit red 10
我可以获得变体ID和产品ID,以及产品名称,如下所示:
<?php
// Get the variations
$args = array( \'post_type\' => \'product_variation\');
$variationloop = new WP_Query( $args );
while ( $variationloop->have_posts() ) : $variationloop->the_post();
// get the parent of each variation
$parent = get_post($post->post_parent);
// is the parent product live?
if ($parent->post_status=="publish")
{
$parentid=$post->post_parent;
echo $parentid; // product
echo $id; // variation id
echo $parent->post_title; // product name
}
?>
但我不知道Woocommerce是如何将变量与属性(如大小)联系起来的。如果我这样做:
$sizes = get_the_terms($parentid ,\'pa_size\');
foreach ( $sizes as $size ) {
echo $size->name;
}
然后我可以得到产品可用的所有尺寸,但我找不到如何检索仅与变体1234相关的尺寸。