如何根据产品类别隐藏缺货项目

时间:2019-08-12 20:52:08

标签: woocommerce product custom-taxonomy

我一直在搜索数小时,但无济于事,我尝试过合并有效的代码

1。仅在商店页面上隐藏缺货的产品:

add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// Only on shop archive pages

if( is_admin() || is_search() || ! is_shop() ) return $meta_query;

$meta_query[] = array(
    'key'     => '_stock_status',
    'value'   => 'outofstock',
    'compare' => '!='
);
return $meta_query;
}

使用

  1. 根据产品类别隐藏产品:

    add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
    function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;
    
    // HERE Define your product category SLUGs to be excluded
    $terms = array( 'ukategorisert' ); // SLUGs only
    
    // The taxonomy for Product Categories
    $taxonomy = 'product_cat';
    
    $tax_query[] = array(
    'taxonomy' => $taxonomy,
    'field'    => 'slug', // Or 'name' or 'term_id'
    'terms'    => $terms,
    'operator' => 'NOT IN', // Excluded
    );
    
    return $tax_query;
    }
    

并尝试将它们组合成下面的代码,但没有成功。 :

    add_filter( 'woocommerce_product_query_meta_query', 'prodcat', 10, 2 );

    function prodcat( $meta_query, $query ) {

  // HERE Define your product category SLUGs to be excluded
    $terms = array( 'pouch' ); // SLUGs only

    // The taxonomy for Product Categories
    $taxonomy = 'product_cat';

    $tax_query[] = array(
    'taxonomy' => $taxonomy,
    'field'    => 'slug', // Or 'name' or 'term_id'
    'terms'    => $terms,
    'operator' => 'NOT IN', // Excluded
);

return $tax_query;

  $meta_query[] = array(
    'key'     => '_stock_status',
    'value'   => 'outofstock',
    'compare' => '!='
);
return $meta_query;


}

请提供一些指导和知识,以帮助根据产品类别隐藏缺货产品。

非常感谢您。

1 个答案:

答案 0 :(得分:0)

我没有意识到我的问题可能模棱两可或不清楚。但是很好,我对代码进行了调整,并设法获得了我想要的结果,即,不是在全球范围内而是根据产品类别来隐藏缺货产品。

这是使用@loictheaztec中的代码完成的。他的代码:

    add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_sold_out_dropdown' );

    add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
    function shop_only_instock_products( $meta_query, $query ) {
// Only on shop archive pages
    if( is_admin() || is_search() || ! is_shop() ) return $meta_query;

$meta_query[] = array(
    'key'     => '_stock_status',
    'value'   => 'outofstock',
    'compare' => '!='
);
return $meta_query;
}

上面的代码是仅在商店页面上隐藏缺货的产品(参考:Hide out of stock products only on shop archive pages in Woocommerce

基本上,我使用他的代码根据特定页面和特定页面隐藏缺货产品,我使用woocommerce的短代码来显示特定产品类别。因此,您去了,我设法根据产品类别隐藏了缺货产品。尽管我必须承认,这是为了达到目标而在丛林中跳动。但是,只要工作完成即可。 :)

所以这是代码:

    add_filter( 'woocommerce_product_query_meta_query', 'prodcat', 10, 2 );

    function prodcat( $meta_query, $query ) {

 if( is_admin() || is_search() || ! is_page( 9173 ) ) return $meta_query;

// is_page ( 'id of page you want to hide the out of stock products, use array() for multiple pages') //

  $meta_query[] = array(
    'key'     => '_stock_status',
    'value'   => 'outofstock',
    'compare' => '!='
);
return $meta_query;


    }