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' );