我使用$variations = $product->get_available_variations();获取产品的所有变量,并将它们作为表显示在描述部分。它适用于大多数产品,但我发现了一个有3个变体的产品,但它显示了其中的两个。我检查了wp_posts表,所有三个变体都与该父产品相关:
SELECT id,post_parent FROM wp_posts WHERE post_parent=843 AND post_type='product_variation';
+-----+-------------+
| id | post_parent |
+-----+-------------+
| 846 | 843 |
| 849 | 843 |
| 852 | 843 |
+-----+-------------+但是,当我使用var_dump($variations);时,数组返回两个ID: 846和849。
这是我的全部职能:
function add_content_after_addtocart_button_func() {
global $product,$post;
if( $product->is_type( 'variable' ) ){
$variations = $product->get_available_variations();
if (count($variations)>0) { ?>
<div class="spb_text_column">
<table class="sf-table standard_minimal">
<tr>
<th>SKU</th>
<th>Length (cm)</th>
<th>Width (cm)</th>
<th>Height (cm)</th>
<th>Price</th>
</tr>
<?php for ($j=0; $j < count($variations); $j++) { ?>
<tr>
<td><?php echo ($variations[$j]['sku'])?$variations[$j]['sku']:$variations[$j]['name'];?></td>
<td><?php echo $variations[$j]['length'];?></td>
<td><?php echo $variations[$j]['width'];?></td>
<td><?php echo $variations[$j]['height'];?></td>
<td><?php echo number_format_i18n($variations[$j]['price']);?></td>
</tr>
<?php } ?>
</table>
</div>
<?php }
}
}请帮助解决这个问题。
发布于 2017-06-19 04:39:25
根据选项'woocommerce_hide_out_of_stock_items‘,股票状态也必须是'instock’。在这种情况下,SQL查询应该是:
SELECT p.ID FROM wp_posts p, wp_postmeta m
WHERE p.ID = m.post_id
AND p.post_parent = 843 AND p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND m.meta_key = '_stock_status' AND m.meta_value = 'instock'此外,过滤器'woocommerce_hide_invisible_variations‘和过滤器'woocommerce_product_is_in_stock’可以修改默认行为。
https://stackoverflow.com/questions/44608411
复制相似问题