2017-11-21 13 views
1

woocommerce 2.3에서는 그룹화 된 제품의 일부인 단일 제품에 대해 post_parent가있었습니다. 따라서 다음과 같이 연결할 수 있습니다.그룹화 된 제품 링크를 woocommerce의 하위 항목 중 하나에서 가져옵니다.

function parent_permalink_button() { 
    global $post; 
    if($post->post_parent != 0){ 
     $permalink = get_permalink($post->post_parent); 
     echo '<a class="button" href="'.$permalink.'">Link to Parent</a>'; 
    } 
} 

woocommerce 3.0.0 업데이트 상황이 변경되었습니다. 사실 그것은 이제 반대입니다. 그룹화 된 제품에는 _children이 있습니다.

단일 제품에서 그룹으로 링크를 만들려면 어떻게해야합니까? 여러 개의 링크가 될 수 있도록 더 많은 그룹화 된 제품의 일부가 될 수 있습니다 (하지만 내 가게의 경우가 아니다)

감사 마이클

답변

1

그것은이 방법 3+ WooCommerce에 대한 해당 기능을 구축하는 것이 가능하다 :
(선택 사양 인 $post_id 인수)

/** 
* Get a button linked to the parent grouped product. 
* 
* @param string (optional): The children product ID (of a grouped product) 
* @output button html 
*/ 
function parent_permalink_button($post_id = 0){ 
    global $post, $wpdb; 

    if($post_id == 0) 
     $post_id = $post->ID; 

    $parent_grouped_id = 0; 

    // The SQL query 
    $results = $wpdb->get_results(" 
     SELECT pm.meta_value as child_ids, pm.post_id 
     FROM {$wpdb->prefix}postmeta as pm 
     INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID 
     INNER JOIN {$wpdb->prefix}term_relationships as tr ON pm.post_id = tr.object_id 
     INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id 
     WHERE p.post_type LIKE 'product' 
     AND p.post_status LIKE 'publish' 
     AND t.slug LIKE 'grouped' 
     AND pm.meta_key LIKE '_children' 
     ORDER BY p.ID 
    "); 

    // Retreiving the parent grouped product ID 
    foreach($results as $result){ 
     foreach(maybe_unserialize($result->child_ids) as $child_id) 
      if($child_id == $post_id){ 
       $parent_grouped_id = $result->post_id; 
       break; 
      } 
     if($parent_grouped_id != 0) break; 
    } 
    if($parent_grouped_id != 0){ 
     echo '<a class="button" href="'.get_permalink($parent_grouped_id).'">Link to Parent</a>'; 
    } 
    // Optional empty button link when no grouped parent is found 
    else { 
     echo '<a class="button" style="color:grey">No Parent found</a>'; 
    } 
} 

코드는 플러그인 파일도 function.php의 활성 자식 테마 (또는 테마)의 파일이나 간다.

는 테스트 및 사용


WooCommerce 3+에서 작동 : 제품 템플릿에서 직접 예를 들어, 선택적 인수 $post_id를 사용하지 않고) (2 건)

1 :

parent_permalink_button(); 

2) Usi

$product_id = 37; // the product ID is defined here or dynamically… 
parent_permalink_button($product_id); 
+0

큰 다음 그것의 주장 $post_id을 정의, 사방 기능을 겨! 그것은 작동합니다. 짧은 설명 템플릿에 추가하고 ($ product-> is_type ('simple')) { $ update_excerpt = "
"인 경우 'simple'제품 유형을 평가하기위한 조건을 추가했습니다. parent_permalink_button(). "

". $ post-> post_excerpt; } –