【问题标题】:Warning: Attempt to read property "term_id" on int - Woocommerce警告:尝试在 int 上读取属性 \"term_id\" - Woocommerce
【发布时间】:2022-08-14 19:36:29
【问题描述】:

我想创建一个条件来手动隐藏商店内的类别。我已经从代码库开始,但我试图理解为什么如果我输入is_product_category() 条件,错误就会出现,因为我在 PHP 版本 8.0 的问题中指定

这是代码:

add_filter( \'get_terms\', \'sct_hide_cat\', 10, 3 );

function sct_hide_cat( $terms, $taxonomies, $args ) {

global $product;
$exclude = [50, 22, 20, 31, 35, 45, 40, 65, 37, 40, 3434];
$new_terms = [];

if ( in_array( \'product_cat\', $taxonomies ) && ! is_admin() && is_shop() || is_product_category() ) {
    foreach ( $terms as $key => $term ) {
        if ( ! in_array( $term->term_id, $exclude ) ) { 
            $new_terms[] = $term;
        }
    }
    $terms = $new_terms;
}
return $terms;
}

    标签: php wordpress woocommerce


    【解决方案1】:

    你可以我认为你应该使用get_terms_args 过滤器而不是get_terms 并添加排除参数,所以现在 get_terms() 函数不会检索那些猫,你会得到正确的计数。这是代码示例:

    add_filter( 'get_terms_args', 'mamaduka_edit_get_terms_args', 10, 2 );
    /**
    * Exclude product categories from Woocommerce
    *
    */
    function mamaduka_edit_get_terms_args( $args, $taxonomies ) {
     //print_r($taxonomies);
    if ( is_admin() && 'product_cat' !== $taxonomies )
        return $args;
    
    $args['exclude'] = [50, 22, 20, 31, 35, 45, 40, 65, 37, 40, 3434]; // Array of cat ids you want to exclude
    return $args;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-26
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2021-04-28
      • 2022-12-13
      • 2021-09-14
      • 2021-10-25
      相关资源
      最近更新 更多