WooCommerce 3.x - 隐藏产品类别

时间:2017-06-13 07:41:37

标签: php wordpress woocommerce

我想知道是否有人知道如何在我的网站上隐藏特定的产品类别。关于我的Wordpress WooCommerce网站的“商店”,“相关产品”和“搜索”的手段。

对于“商店”页面,我已完成(并且正在运作)以下内容:

 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:我需要保留此类别,因为我正在使用它来创建一些复合产品,所以只能隐藏这些产品但不能删除它们。

干杯

3 个答案:

答案 0 :(得分:2)

从搜索中排除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' );

答案 1 :(得分:0)

要从搜索和SHOP页面中排除您的类别(&#34; Carton&#34;此处),请在我的子主题的 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' );

关于&#34;相关产品&#34;,我不得不调整它,但最终它改善了与woocommerce的相关性。我基本上按品牌,类别和顺序排序我的相关产品。季节。因为&#34; Carton&#34;是一个类别,它不会检索任何类别:

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>

答案 2 :(得分:0)

如果要完全禁用产品类别(和产品标签),可以在functions.php文件中使用以下过滤器:

add_filter('get_the_terms', function ($terms, $id, $taxonomy) {
    if ($taxonomy === 'product_cat' || $taxonomy === 'product_tag') {
        return [];
    }
    return $terms;
}, 99, 3);