Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WooCommerce产品类别小部件中过滤计数_Woocommerce - Fatal编程技术网

如何在WooCommerce产品类别小部件中过滤计数

如何在WooCommerce产品类别小部件中过滤计数,woocommerce,Woocommerce,我想过滤WooCommerce的“产品类别”小部件中出现的计数。我根据用户角色隐藏某些产品,例如类别“Widgets”总共有5个产品,但给定用户可能只看到其中的1个。因此,我希望类别显示为“Widgets(1)”而不是“Widgets(5)” 我的第一个想法是过滤获取\u terms。然而,由于某种原因,无论我做什么,计数都不会改变。我可以改变其他东西,比如名字,但不能改变计数 例如——假设我有两个类别:酒精(4)和小部件(5)。我添加以下代码: add_filter( 'get_terms',

我想过滤WooCommerce的“产品类别”小部件中出现的计数。我根据用户角色隐藏某些产品,例如类别“Widgets”总共有5个产品,但给定用户可能只看到其中的1个。因此,我希望类别显示为“Widgets(1)”而不是“Widgets(5)”

我的第一个想法是过滤
获取\u terms
。然而,由于某种原因,无论我做什么,计数都不会改变。我可以改变其他东西,比如名字,但不能改变计数

例如——假设我有两个类别:酒精(4)和小部件(5)。我添加以下代码:

add_filter( 'get_terms', 'test_20200822' ), 10, 2 );
function test_20200822( $terms, $taxonomy ) {
    if ( in_array( 'product_cat', $taxonomy ) ) {
        foreach ( $terms as $i => $term ) {
            if ( is_a( $term, 'WP_Term' ) ) {
                // prepend an "X" to the name... this works correctly:
                $terms[$i]->name = 'X'.$terms[$i]->name;
                // change the count to 1... this doesn't work:
                $terms[$i]->count = 1;
            }
        }
    }
    return $terms;
}
结果是:

如您所见,名称已更改,但计数未更改。我错过了什么

注1:无论这是什么,都是特定于WooCommerce的。如果我在常规WordPress类别上使用相同的代码,它工作正常,并按预期更改计数


注2:我在这里问这个问题,因为WordPress StackExchange说WooCommerce在这里是离题的。

好吧,在我熟睡之后,我明白了。将钩子的优先级更改为大于10可以使其工作:facepalm:

这是因为WooCommerce本身在优先级10处钩住了
get_terms
,并覆盖了term counts。在文件
woocommerce/includes/wc term functions.php
中找到:

/**
 * Overrides the original term count for product categories and tags with the product count.
 * that takes catalog visibility into account.
 *
 * @param array        $terms      List of terms.
 * @param string|array $taxonomies Single taxonomy or list of taxonomies.
 * @return array
 */
function wc_change_term_counts( $terms, $taxonomies ) {
    if ( is_admin() || is_ajax() ) {
        return $terms;
    }

    if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ), true ) ) {
        return $terms;
    }

    $o_term_counts = get_transient( 'wc_term_counts' );
    $term_counts   = $o_term_counts;

    foreach ( $terms as &$term ) {
        if ( is_object( $term ) ) {
            $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_term_meta( $term->term_id, 'product_count_' . $taxonomies[0], true );

            if ( '' !== $term_counts[ $term->term_id ] ) {
                $term->count = absint( $term_counts[ $term->term_id ] );
            }
        }
    }

    // Update transient.
    if ( $term_counts !== $o_term_counts ) {
        set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 );
    }

    return $terms;
}
add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );

在这里提到这一点,以防其他人也会遇到同样的情况。

好吧,睡了一觉,我就明白了。将钩子的优先级更改为大于10可以使其工作:facepalm:

这是因为WooCommerce本身在优先级10处钩住了
get_terms
,并覆盖了term counts。在文件
woocommerce/includes/wc term functions.php
中找到:

/**
 * Overrides the original term count for product categories and tags with the product count.
 * that takes catalog visibility into account.
 *
 * @param array        $terms      List of terms.
 * @param string|array $taxonomies Single taxonomy or list of taxonomies.
 * @return array
 */
function wc_change_term_counts( $terms, $taxonomies ) {
    if ( is_admin() || is_ajax() ) {
        return $terms;
    }

    if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ), true ) ) {
        return $terms;
    }

    $o_term_counts = get_transient( 'wc_term_counts' );
    $term_counts   = $o_term_counts;

    foreach ( $terms as &$term ) {
        if ( is_object( $term ) ) {
            $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_term_meta( $term->term_id, 'product_count_' . $taxonomies[0], true );

            if ( '' !== $term_counts[ $term->term_id ] ) {
                $term->count = absint( $term_counts[ $term->term_id ] );
            }
        }
    }

    // Update transient.
    if ( $term_counts !== $o_term_counts ) {
        set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 );
    }

    return $terms;
}
add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );
在这里提到这一点,以防其他任何人最终也会遇到同样的情况