以下代码适用于WooCoommerce 1。x-它不太可能在任何最新版本上工作,我非常鼓励您not to bother trying to use it. 我也强烈建议你don\'t 降级至WooCommerce 1。x以使用此代码。
这并不容易,但我和一位同事做到了。
其中一些是特定于我们的用例的,在较大的目录上不会表现得很好,但在技术上是可行的。由于在提取项目之前无法判断情况是否如此,因此我们在一个页面上显示所有项目,只需在缺货项目上显示:无。同样,这是一种变通方法,但符合我们的目的。
function product_in_stock($post, $product) {
    if ($_GET[\'filtering\'] == 1 && $_GET[\'filter_size\'] > 0 ) {
        $STOCK = FALSE;
        $slugmap = array();
        $attribs = $product->get_variation_attributes();
        $terms = get_terms( sanitize_title( \'pa_size\' ));
        if($terms)foreach($terms as $term)$slugmap[$term->slug]=$term->term_id;
        $available = $product->get_available_variations();
        if($available)foreach($available as $instockitem){
            if(isset($instockitem["attributes"]["attribute_pa_size"])){
                if(isset($slugmap[$instockitem["attributes"]["attribute_pa_size"]])){
                    if($slugmap[$instockitem["attributes"]["attribute_pa_size"]] == $_GET["filter_size"]){
                        $STOCK = TRUE;
                    }
                }
            }
        }
        return $STOCK;
    } else {
        return true;
    }
}
function check_if_out_of_stock(){
    global $post,$product;
    $stock = product_in_stock($post,$product);
    $output = \'<div class="\';
    $output .= $stock?"instock":"outofstock";
    $output .= \'">\';
    echo $output;
}
add_action( \'woocommerce_before_shop_loop_item\', \'check_if_out_of_stock\');
function close_out_of_stock(){
    echo "</div>";
}
add_action( \'woocommerce_after_shop_loop_item\', \'close_out_of_stock\');