首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在wordpress中隐藏脱销产品?

如何在wordpress中隐藏脱销产品?
EN

Stack Overflow用户
提问于 2021-10-23 09:38:14
回答 1查看 56关注 0票数 0

我需要从最新或最新的产品和最畅销或最畅销的产品中隐藏脱销的产品,我已经用这个代码为相关的产品做了这件事

代码语言:javascript
复制
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' );
} );

我该如何做才能获得最新和最畅销的产品?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-23 10:00:50

您可以使用pre_get_posts操作挂钩。来隐藏脱销的产品。

is_product_category() -查看产品类别存档时返回true。

is_product_category( 'shirts' ) -当“衬衫”类别的产品类别页面正在显示时。

is_product_category( array( 'shirts', 'games' ) ) -当“衬衫”或“游戏”类别的产品类别页面正在显示时。

代码语言:javascript
复制
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' );

}

查看更多Conditional Tags

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69686891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档