2017-06-13 210 views
0

我想知道是否有人對如何在我的網站上隱藏特定產品類別有所瞭解。意味着在我的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:我需要保留這個類別,因爲我使用它來創建一些複合產品,所以只隱藏這些產品但不刪除它們。

乾杯

+0

嗨,你有答案嗎?我看起來一樣。 – mikesneider

+0

嗨@mikesneider我已經發布了一個答案。是的,我終於明白了這一點;) 如果有任何問題,隨時問和大拇指,如果它適合你! – bkseen

回答

0

從搜索和店鋪頁面排除的類別( 「箱」 在這裏),在我的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> 
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'); 
相關問題