首页 文章

在Woocommerce单品页面上排除特定的产品类别

提问于
浏览
2

我正在尝试将某些类别排除在WooCommerce产品页面上 .

示例:如果在单个产品页面中我有"Categories: Cat1, Cat" 2“,我希望只显示Cat1 .

我尝试在单一产品模板中编辑meta.php . 我创建了一个新功能:

$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;

然后我得到了WooCommerce的回音:

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

但它仍然显示相同的类别 . 奇怪的是,当我做一个 var_dump($categories) 它显示正确的事情 .

3 回答

  • 2

    您应该尝试使用 get_the_terms 过滤器钩子中挂钩的自定义函数,这将排除要在单个产品页面上显示的特定产品类别:

    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;
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 .

    经过测试和工作 .

  • 0

    Try this:

    将以下代码添加到 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;
    }
    
  • 0

    您可以通过在 get_terms 钩子上添加过滤器,然后在单个产品页面 and 上运行 get_terms() ,然后从术语列表中排除上述类别ID来实现此目的,如果要获取的术语是 product_cat .

    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;
    }
    

    您可以将此代码添加到 functions.php .

相关问题