我需要从最新或最新的产品和最畅销或最畅销的产品中隐藏缺货产品我已经为相关产品做了这个代码
function hide_out_of_stock_option( $option ){
return 'yes';
}
add_action( 'woocommerce_before_template_part', function( $template_name ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'hide_out_of_stock_option' );
} );
add_action( 'woocommerce_after_template_part', function( $template_name ) {
if( $template_name !== "single-product/related.php" ) {
return;
}
remove_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'hide_out_of_stock_option' );
} );
我该怎样做才能获得最新最畅销的产品呢?
可以使用pre_get_posts
动作钩子。隐藏缺货产品。
is_product_category()
-在查看产品类别存档时返回true。
is_product_category( 'shirts' )
-当"衬衫"类别的产品类别页面显示时。
is_product_category( array( 'shirts', 'games' ) )
-当"衬衫"或"游戏"类别的产品类别页面显示时。
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function hide_outofstock_products( $q ) {
if ( ! is_admin() && is_product_category('your category slug') ) {
$q->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'hide_outofstock_products' );
}
查看更多条件标签