Exclude specific product categories on Woocommerce single product pages

空扰寡人 提交于 2020-06-26 06:51:13

问题


I'm trying to exclude some categories from being displayed on the WooCommerce product page.

Example: if in a single product page I have "Categories: Cat1, Cat"2", I want that only Cat1 will be displayed.

I tried editing the meta.php in the single-product template. I created a new function:

$categories = $product->get_category_ids();
$categoriesToRemove = array(53,76,77,78); // my ids to exclude
foreach ( $categoriesToRemove as $categoryKey => $category) {
    if (($key = array_search($category, $categories)) !== false) {
        unset($categories[$key]);
    }
}
$categoriesNeeded = $categories;

Then I have the echo from WooCommerce:

echo wc_get_product_category_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count($categories), 'woocommerce' ) . ' ', '</span>' );

But it still shows the same categories. The strange thing is that when I do a var_dump($categories) it shows the correct thing.


回答1:


You should try this custom function hooked in get_the_terms filter hook, that will exclude specific product categories to be displayed on single product pages:

add_filter( 'get_the_terms', 'custom_product_cat_terms', 20, 3 );
function custom_product_cat_terms( $terms, $post_id, $taxonomy ){
    // HERE below define your excluded product categories Term IDs in this array
    $category_ids = array( 53,76,77,78 );

    if( ! is_product() ) // Only single product pages
        return $terms;

    if( $taxonomy != 'product_cat' ) // Only product categories custom taxonomy
        return $terms;

    foreach( $terms as $key => $term ){
        if( in_array( $term->term_id, $category_ids ) ){
            unset($terms[$key]); // If term is found we remove it
        }
    }
    return $terms;
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works.




回答2:


Try this:

add the below code to single-product.php

add_filter( 'get_terms', 'organicweb_exclude_category', 10, 3 );
function organicweb_exclude_category( $terms, $taxonomies, $args ) {
  $new_terms = array();
  // if a product category and on a page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page() ) {
    foreach ( $terms as $key => $term ) {
// Enter the name of the category you want to exclude in place of 'uncategorised'
      if ( ! in_array( $term->slug, array( 'uncategorised' ) ) ) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}



回答3:


You can achieve this by adding a filter on the get_terms hook, and then excluding the above-mentioned category ID's from the terms list if get_terms() was run on a single product page and if the terms being fetched are product_cat's.

add_filter( 'get_terms', 'danski_single_product_exclude_category', 10, 3 );
function danski_single_product_exclude_category( $terms, $taxonomies, $args ) {
    $new_categories = array();
    // if a product category and a single product
    if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_product() ) {
        foreach ( $terms as $key => $term ) {
            if ( ! in_array( $term->term_id, array( 53,76,77,78 ) ) ) { //add the category id's that you want to exclude here
                $new_categories[] = $term;
            }
        }
        $terms = $new_categories;
    }
    return $terms;
}

You can add this code to your functions.php.



来源:https://stackoverflow.com/questions/48498155/exclude-specific-product-categories-on-woocommerce-single-product-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!