dtdsbakn210537 2018-05-29 12:23
浏览 66
已采纳

以默认语言获取产品类别的术语计数(使用WPML)

I'm using WPML as translation plugin and Woocommerce. My products are only in English and the other languages fall back to these without translation. However, I'm facing an issue with product categories count. If the website language is English and e.g. in Cat A I have 4 products. The count is displayed correct with 4. If, I switch to Italian, the count is 0. I already checked with WPML Support. They advised me to use customized code. So, I created a shortcode to display all categories with the counter as this do the official shortcode of Woocmmerce [product_categories]. With my own shortcode the categories are displayed, but I have still same issue. Here is my code. I have an if (ICL Language Code == '') then get the default category code. I tested this and I get the default category ID. With the category code I try to get the term count. However, there seems to be something wrong. Even $default_lang_id is the category ID in English, the counter fall back to the current language, e.g. Italian. Does I need to feed this by a wp query directly from terms_taxonomies -> count?

 function prod_categories() {
$get_featured_cats = array(
    'taxonomy'     => 'product_cat',
    'orderby'      => 'asc',
    'show_count'   => '1',
    'hide_empty'   => '0',
    'include'      => $cat_array
);

$all_categories = get_categories( $get_featured_cats );
$j = 1;
foreach ($all_categories as $cat) {
    $cat_id   = $cat->term_id;
    $cat_link = get_category_link( $cat_id );

    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); // Get Category Thumbnail
    $image = wp_get_attachment_url( $thumbnail_id ); 
    if(ICL_LANGUAGE_CODE=='de' || ICL_LANGUAGE_CODE=='fr' || ICL_LANGUAGE_CODE=='it'){
    $default_lang_id = icl_object_id( $cat_id, 'product_cat', true, 'en' );
    $default_lang_count = get_term( $default_lang_id, 'product_cat' );

};
    if ( $image ) {
        echo '<div style="float:left;margin:10px;"><a href="' . $cat_link . '"><img src="' . $image . '" alt="" /><br><center>' . $cat->name . ' ('. $cat->count .')</center></a>'.$default_lang_id. ' ' .'('.$default_lang_count->count.')</div>';
    }
    $j++;
}
// Reset Post Data
wp_reset_query();

}
add_shortcode( 'prod_categories', 'prod_categories' );

Please see here screenshot for better understanding enter image description here

  • 写回答

1条回答 默认 最新

  • dongqichang7988 2018-05-29 12:35
    关注

    Since 4.8 version WordPress supports "suppress_filter" parameter for get_terms function. So, i guess the missed point is that filter. This parameter should force the function to ignore "WPML existence factor" and to fetch all category data as they are.

    Try to add that and test it again:

    $get_featured_cats = array(
        'taxonomy'     => 'product_cat',
        'orderby'      => 'asc',
        'show_count'   => '1',
        'hide_empty'   => '0',
        'include'      => $cat_array,
        'suppress_filter'=>true
    );
    

    Alternative way: (via SQL query)

        //$default_lang_id = icl_object_id( $cat_id, 'product_cat', true, 'en' );
        //$default_lang_count = get_term( $default_lang_id, 'product_cat' );
        global $wpdb;
        $default_lang_count=$wpdb->get_var("select count from 
        $wpdb->term_taxonomy 
        where term_id=".esc_sql($cat_id));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?