繁体   English   中英

WooCommerce单品页面隐藏“缺货”相关产品

[英]Hide “out of stock” related products on WooCommerce single product page

我需要一点帮助,拜托。 我想从单个产品页面中隐藏不再库存的产品,仅在相关产品区域中。 我创建了它,一切正常,但我无法将它隐藏在相关区域,我所做的任何更改都不起作用。 现有代码在我的情况下不起作用。 谢谢你。

我用这些来创建“缺货”徽章:

//Add an out of stock overlay to product images when all variations are unavailable
add_action( 'woocommerce_before_shop_loop_item_title', function() {
    global $product;
    if ( !$product->is_in_stock() ) {
        echo '<span class="sold-out-overlay">Stoc epuizat</span>';
    }
});

 /*Sold out badge automat in*/.sold-out-overlay { background: #000000; color: #fff; font-size: 11px; font-weight: 600; padding: 5px 10px; position: absolute; right: 5px; top: 20px; z-index: 1; }

无需使用 CSS,您可以使用woocommerce_related_products过滤器挂钩。

function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {    
    foreach( $related_posts as $key => $related_post ) {        
        // Get product
        $related_product = wc_get_product( $related_post );
        
        // Is a WC product 
        if ( is_a( $related_product, 'WC_Product' ) ) {
            // Stock status
            $stock_status = $related_product->get_stock_status();
            
            // Out of stock
            if ( $stock_status == 'outofstock' ) {
                unset( $related_posts[$key] );
            }
        }
    }
    
    return $related_posts;
}
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );

相关:在 WooCommerce 单品页面仅显示“有货”相关产品

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM