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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP