2016-09-06 5 views
0

Wordpress 4.6, WPML 3.5, WooCommerce Multilingual 3.8.6 및 WooCommerce 버전 2.6.4를 사용하여 WooCommerce 다중 언어 저장소를 개발 중입니다.WMPL에서 특정 언어의 WooCommerce 제품 카테고리를 가져옵니다.

코드는 항상 주 언어로 범주를 반환하지만 지정된 언어에 대한 번역은 반환하지 않습니다. 그래서 제거 그것은 WPML 자동 분류 체계의 ID를 조정하는 것이 밝혀졌다

https://wpml.org/forums/topic/how-to-get-the-translated-taxonomy-object/

:이 링크의 해결책을 발견

private $lang; 

    function __construct($lang = "en") { 
     $this->lang = $lang; 
    } 

    private function getCategories() { 
     try { 
       $api = WC()->api->WC_API_Products; 
       $categories = $api->get_product_categories(); 

       $products_categories = $categories["product_categories"]; 

       foreach($products_categories as $category) { 
        $id = absint($category["id"]); 
        $category["name"] = $this->get_translated_term_name($id, "product_cat", $this->lang); 
       } 

       return $products_categories; 

      } catch (Exception $e) { 
       error_log("Caught $e"); 
      } 
    } 

    private function get_translated_term_name($term_id, $taxonomy, $language) { 

     $translated_term_id = icl_object_id($term_id, $taxonomy, true, $language); 

     $translated_term_object = get_term_by('id', $translated_term_id, $taxonomy); 

     return $translated_term_object->name; 
    } 

답변

0
<?php 
function wpa89819_wc_single_product(){ 

    $product_cats = wp_get_post_terms(get_the_ID(), 'product_cat'); 

    if ($product_cats && ! is_wp_error ($product_cats)){ 

     $single_cat = array_shift($product_cats); ?> 

     <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2> 

<?php } 
} 
add_action('woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2); 
+0

private function getTerm($id) { global $wpdb; $id = absint($id); if(!$id) { return false; } $term = $wpdb->get_results("SELECT * FROM $wpdb->terms WHERE term_id=$id"); return $term; 

답장 주셔서 감사합니다,하지만 난 HTML로 후크 및 출력을 범주를 사용하지 않으려는 API 요청 – Waxren

0

- : 여기

코드입니다 get_term을 호출하기 전에 필터가 문제를 해결했습니다. 코드는 다음과 같습니다.

private function get_translated_term_name($term_id, $taxonomy, $language) { 

     global $sitepress; 
     remove_filter('get_term', array($sitepress,'get_term_adjust_id'), 1, 1); 

     $translated_term_id = icl_object_id($term_id, $taxonomy, true, $language); 

     $translated_term_object = get_term($translated_term_id, $taxonomy); 

     add_filter('get_term', array($sitepress,'get_term_adjust_id'), 1, 1); 

     return $translated_term_object->name; 
} 
0

또한 get_term 지원 기능의 동작에 놀랄 수도 있습니다. 때로는 제공된 term_id에 해당하는 용어를 입력 매개 변수로 반환하지 않지만 캐시 된 결과를 반환합니다! 그래서 저는 이것을 우회하여 이렇게 만들었습니다. 이에 json으로 respone로 다시 전송되기 때문에 내가 배열로 모든 범주를 원}

관련 문제