【问题标题】:woocommerce get_available_variations doesn't work properlywoocommerce get_available_variations 无法正常工作
【发布时间】:2017-06-17 19:07:37
【问题描述】:

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


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 }
    }

}

请帮忙解决这个问题。

【问题讨论】:

  • 你是从后端设置产品数量吗?如果没有,请设置产品数量。
  • @OmarFaruque,是的,已经设置好了。除价格和 SKU 外,所有变体都具有相同的设置和值

标签: php wordpress woocommerce


【解决方案1】:

根据选项“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”可以修改默认行为。

【讨论】:

  • 所有 3 个变体都有 100 个库存,其库存状态为“有货”。在 woocommerce 设置中也禁用了“从目录中隐藏缺货商品”
  • 是否安装了“woocommerce_hide_out_of_stock_items”、“woocommerce_hide_invisible_variations”或“woocommerce_product_is_in_stock”过滤器?
  • 您还可以验证变体的 post_status 是否为“发布”。
  • 另一个要检查的过滤器是“woocommerce_variable_children_args”。
  • 关于过滤器:不,我在 function.php 文件中没有找到任何内容。
猜你喜欢
  • 2015-01-22
  • 1970-01-01
  • 2019-02-22
  • 2016-12-01
  • 2018-10-24
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多