我在WC设置中禁用了HIDE OUT OF STOCK产品,但是即使某些产品(每种产品都有零库存),其中某些产品(主要是可变产品)仍在目录中显示。我尝试使用此代码隐藏脱销的产品,但无法正常工作。是否有任何解决方法?谢谢。
add_action( 'woocommerce_before_shop_loop_item_title', 'wk_out_of_stock_variations_loop' );function wk_out_of_stock_variations_loop(){
global $product;
if ( $product->product_type == 'variable' ) { // if variation product is out of stock
$available = $product->get_available_variations();
if ( $available )foreach ( $available as $instockvar ) {
if ( isset($instockvar['attributes']['attribute_pa_megethos'] ) ) {
if(isset($_GET['filter_megethos'])){
$destostock = $_GET['filter_megethos'];
$array = explode(',', $destostock);
if (in_array($instockvar['attributes']['attribute_pa_megethos'], $array )&& (!$instockvar['max_qty']>0)){
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
else{
if (in_array($instockvar['attributes']['attribute_pa_megethos'], $array )&& ($instockvar['max_qty']>0)){
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: block !important}</style>";
}
}
}
}