首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >woocommerce get_available_variations不能正常工作

woocommerce get_available_variations不能正常工作
EN

Stack Overflow用户
提问于 2017-06-17 19:07:37
回答 1查看 3.2K关注 0票数 1

我使用$variations = $product->get_available_variations();获取产品的所有变量,并将它们作为表显示在描述部分。它适用于大多数产品,但我发现了一个有3个变体的产品,但它显示了其中的两个。我检查了wp_posts表,所有三个变体都与该父产品相关:

代码语言:javascript
复制
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。

这是我的全部职能:

代码语言:javascript
复制
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 }
    }

}

请帮助解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2017-06-19 04:39:25

根据选项'woocommerce_hide_out_of_stock_items‘,股票状态也必须是'instock’。在这种情况下,SQL查询应该是:

代码语言:javascript
复制
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’可以修改默认行为。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44608411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档