WooCommerce 3.x - 隐藏产品类别
问题描述:
我想知道是否有人对如何在我的网站上隐藏特定产品类别有所了解。意味着在我的Wordpress WooCommerce网站的“商店”,“相关产品”和“搜索”。WooCommerce 3.x - 隐藏产品类别
对于我做的“商店”页面(它的工作)如下:
function custom_pre_get_posts_q($q) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('carton'), // Don't display products in the composite category on the shop page.
'operator' => 'NOT IN'
);
$q->set('tax_query', $tax_query);
}
add_action('woocommerce_product_query', 'custom_pre_get_posts_q');
对于搜索我尝试以下,但它不工作:
function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID('carton');
$query->set('cat', '-'.$cat_id);
}
return $query;
}
add_filter('pre_get_posts','exclude_category_from_search');
最后,对于相关产品我试过,因为WC 3.X这似乎不建议使用以下:
function wc_remove_related_products($args)
{
if (is_product() && has_term('carton', 'product_cat'))
{
return array();
}
return $args;
}
add_filter('woocommerce_related_products_args','wc_remove_related_products', 10);
我也有我的孩子主题如下:
` <?php foreach ($related_products as $related_product) : ?>
<?php
$post_object = get_post($related_product->get_id());
setup_postdata($GLOBALS['post'] =& $post_object);
wc_get_template_part('content', 'product'); ?>
<?php endforeach; ?>`
而且我知道我们可以用我在其他类中使用的这部分代码隐藏产品类别:
global $post;
$terms = wp_get_post_terms($post->ID, 'product_cat');
foreach ($terms as $term) $categories[] = $term->slug;
if (in_array('children', $categories)) {
任何人都有关于如何使用新版本的WoomCommerce做到这一点的想法? 我已经做了大量的研究,但是从这个新版本开始,所有看起来都是不赞成的答案。 PS:我需要保留这个类别,因为我使用它来创建一些复合产品,所以只隐藏这些产品但不删除它们。
干杯
答
从搜索和店铺页面排除的类别( “箱” 在这里),在我的function.php中加入以下内容我的孩子主题:
/* hide CARTON category SEARCH
=============================*/
function sm_pre_get_posts($query) {
if ( $query->is_search()) {
$query->set('post_type', array('product'));
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set('tax_query', $tax_query);
}
}
add_action('pre_get_posts', 'sm_pre_get_posts');
/* hide CARTON category for SHOP pages
====================================*/
function custom_pre_get_posts_query($q) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('carton'), // Don't display products in the carton category on the shop page.
'operator' => 'NOT IN'
);
$q->set('tax_query', $tax_query);
}
add_action('woocommerce_product_query', 'custom_pre_get_posts_query');
关于“相关产品”,我不得不调整它,但最终它正在改善与woocommerce相关的未来。我基本上是按品牌顺序排序我的相关产品,类别&季节。因为“纸箱”是一个类别,它不检索任何一个:
global $post;
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$myTerm = $term->slug;
if($myTerm == 'mens' || $myTerm == 'ladies'){
$mainCat = $myTerm;
}else if($myTerm == 'accessories'){
$mainCat = $myTerm;
}else if($myTerm == 'children'){
$mainCat = $myTerm;
}else if($myTerm == 'summer' || $myTerm == 'winter'){
$seasonCat = $myTerm;
}
}
$terms = get_the_terms(get_the_ID(), 'pa_brand');
foreach ($terms as $term) {
$theBrand = $term->slug;
}
?>
<div class="product-carousel related-products spb_content_element">
<div class="title-wrap clearfix">
<h3 class="spb-heading"><span><?php echo esc_attr($related_heading); ?></span></h3>
<div class="carousel-arrows"><a href="#" class="carousel-prev"><i class="sf-icon-chevron-prev"></i></a><a href="#" class="carousel-next"><i class="sf-icon-chevron-next"></i></a></div>
</div>
<div class="related products carousel-items <?php echo esc_attr($gutter_class); ?> product-type-<?php echo esc_attr($related_product_display_type); ?>" id="carousel-<?php echo esc_attr($sf_carouselID); ?>" data-columns="<?php echo esc_attr($woocommerce_loop['columns']); ?>">
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $theBrand
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $mainCat
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $seasonCat
)
),
'post_type' => 'product',
'orderby' => 'rand',
'order' => 'desc',
'posts_per_page' => 12
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
wc_get_template_part('content', 'product');
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata(); ?>
</div>
</div>
答
排除搜索
woocommerce类添加以下功能function.php文件
function sm_pre_get_posts($query) {
if (! is_admin() && $query->is_main_query() && $query->is_search()) {
$query->set('post_type', array('product'));
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set('tax_query', $tax_query);
}
}
add_action('pre_get_posts', 'sm_pre_get_posts');
嗨,你有答案吗?我看起来一样。 – mikesneider
嗨@mikesneider我已经发布了一个答案。是的,我终于明白了这一点;) 如果有任何问题,随时问和大拇指,如果它适合你! – bkseen