从产品标签内的特定产品类别中排除Woocommerce产品

时间:2017-11-22 17:29:45

标签: php wordpress woocommerce tags product

如何从“备件”类别中排除产品以显示在woocommerce产品标签内?我想列出/ tags /中的所有产品,但如果该产品属于“备件”类别,则只需将字体列入其中。我需要使用哪种功能。

我在网上搜索了这个,但它确实有效。

1 个答案:

答案 0 :(得分:1)

在这个挂钩在"spare-parts"过滤器钩子中的自定义函数中,这可以轻松完成。

  

因此, add_filter( 'woocommerce_product_query_tax_query', 'exclude_specific_product_category_query', 10, 2 ); function exclude_specific_product_category_query( $tax_query, $query ) { // Only on Product Tag archives pages if( is_admin() || ! is_product_tag() ) return $tax_query; // HERE Define your product category SLUGs to be excluded $terms = array( 'spare-parts' ); // SLUGs only // The taxonomy for Product Categories $taxonomy = 'product_cat'; // Add your criteria $tax_query[] = array( 'taxonomy' => $taxonomy, 'field' => 'slug', // Or 'name' or 'term_id' 'terms' => $terms, 'operator' => 'NOT IN', // Excluded ); return $tax_query; } 产品类别 slug 的所有产品都将从产品代码存档页面中排除。

以下是代码:

WP_query

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作。

因此,如果您需要在 tax_query 中使用此功能,您将使用类似的 // HERE Define your product category SLUGs to be excluded $terms_cat = array( 'spare-parts' ); // SLUGs only // HERE Define your product tag SLUGs to be included $terms_tag = array( 'special', 'wear' ); // SLUGs only // The taxonomies $taxonomy_tag = 'product_tag'; // Product tag $taxonomy_cat = 'product_cat'; // Product Category // The Query $loop = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'order' => 'ASC', 'tax_query' => array( 'relation' => 'AND', array( // The tag query (Include) 'taxonomy' => $taxonomy_tag, 'field' => 'slug', 'terms' => $terms_tag, ), array( // the category query (exclude) 'taxonomy' => $taxonomy_cat, 'field' => 'slug', // Or 'name' or 'term_id' 'terms' => $terms_cat, 'operator' => 'NOT IN', // Excluded ), ) )); if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post(); $products_ids[] = $loop->post->ID; // Set the product Ids in an array endwhile; endif; // always reset the WP_Query wp_reset_query(); // Convert the array in a string with all product Ids coma separated $product_ids_string = implode( ', ', $products_ids ); // Output Ids echo $product_ids_string; ,其中您将排除某些产品类别和包括一些产品标签......

在此示例中,它将输出以逗号分隔的产品ID字符串:

return s.Length - s.Replace(x.ToString(),"").Length;

经过测试和工作。

相关文档:apply

相关问题