2014-03-12 4 views
2

에 의해 그룹 다운로드 (* 내 계정 페이지) 내가 좋아하는, 사용 가능한 모든 다운로드에 지금 정렬되지 않은 목록을 볼 수 있습니다 :WooCommerce - Woocommerce에서 제품

<ul class="digital-downloads"> 
    <li><a href="#">Product 1 - File</a></li> 
    <li><a href="#">Product 1 - Another File</a></li> 
    <li><a href="#">Product 2 - File</a></li> 
    <li><a href="#">Product 2 - Another File</a></li> 
    <li><a href="#">Product 3 - File</a></li> 
    <li><a href="#">Product 3 - Another File</a></li> 
</ul> 

내가 어떻게 그룹 제품으로 다운로드? 같은 :

<ul class="digital-downloads"> 
    <li> 
    <span>Product 1</span> 
    <ul> 
     <li><a href="#">File</a></li> 
     <li><a href="#">Another File</a></li> 
    </ul> 
    </li> 

    <li> 
    <span>Product 2</span> 
    <ul> 
     <li><a href="#">File</a></li> 
     <li><a href="#">Another File</a></li> 
    </ul> 
    </li> 

    <li> 
    <span>Product 3</span> 
    <ul> 
     <li><a href="#">File</a></li> 
     <li><a href="#">Another File</a></li> 
    </ul> 
    </li> 
</ul> 

내 테마/woocommerce/내 - 계정/내 - downloads.php에서 코드 : 내가 아는

<ul class="digital_downloads"> 
    <?php foreach ($downloads as $download) : ?> 
     <li> 
      <?php 
       do_action('woocommerce_available_download_start', $download); 

       echo apply_filters('woocommerce_available_download_link', '<a href="' . esc_url($download['download_url']) . '">' . $download['download_name'] . '</a>', $download); 

       do_action('woocommerce_available_download_end', $download); 
      ?> 
     </li> 
    <?php endforeach; ?> 
</ul> 

답변

1

, 나는이 질문에 매우 늦게 대답하고 있지만 다른 사람들이 답을 찾지 못하는 경우를 대비하여 답변을 게시하고 있습니다.

하위 테마를 만들고 해당 하위 테마에 /woocommerce/my-account/my-downloads.php 파일을 만드십시오. 코드 위

<?php 
/** 
* My Orders 
* 
* Shows recent orders on the account page 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  2.0.0 
*/ 
if (!defined('ABSPATH')) { 
    exit; 
} 

function wdm_print_download_file_name($download, $product_meta) { 
    if (is_numeric($download['downloads_remaining'])) 
     echo apply_filters('woocommerce_available_download_count', '<span class="count">' . sprintf(_n('%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce'), $download['downloads_remaining']) . '</span> ', $download); 

    echo apply_filters('woocommerce_available_download_link', '<a href="' . esc_url($download['download_url']) . '">' . $product_meta[$download['download_id']]['name'] . '</a>', $download); //Print file name 
} 

if ($downloads = WC()->customer->get_downloadable_products()) : 
    ?> 

    <h2><?php echo apply_filters('woocommerce_my_account_my_downloads_title', __('Available downloads', 'woocommerce')); ?></h2> 

    <ul class="digital-downloads"> 
     <?php 
     $all_product_ids = array(); 
     $all_download_ids = array(); 
     $download_ids_used_till_now = array(); 
     foreach ($downloads as $download) : 
      ?> 

      <?php 
      do_action('woocommerce_available_download_start', $download); 

      if (!in_array($download['product_id'], $all_product_ids)) { //Check if current product id is already there in $all_product_ids array. If it goes in this loop, that means it is new product id 
       $product_meta = get_post_meta($download['product_id'], '_downloadable_files', true); //All download ids of the product 

       $all_product_ids[] = $download['product_id']; //Push Current download's Product id to an array 

       $all_download_ids = array_keys($product_meta); //Get all download ids of the current product 

       echo '<li><span>' . get_the_title($download['product_id']) . '</span><ul>'; //Print product name 

       $download_ids_used_till_now[] = $download['download_id']; //add download id to an array 

       echo '<li>'; 
       wdm_print_download_file_name($download, $product_meta); 
       echo '</li>'; 

       $check_if_this_is_last_element = array_values(array_diff($all_download_ids, $download_ids_used_till_now)); 

       if (empty($check_if_this_is_last_element)) { //This is last download id of a product 
        echo '</ul></li>'; //close ul and li tags 
       } 
      } else { //product id is already in use. 
       $check_if_this_is_last_element = array_values(array_diff($all_download_ids, $download_ids_used_till_now)); 

       if (isset($check_if_this_is_last_element[0]) && $download['download_id'] == $check_if_this_is_last_element[0]) { //This is last download id of a product 
        echo '<li>'; 
        wdm_print_download_file_name($download, $product_meta); 
        echo '</li>'; 

        echo '</ul></li>'; //close ul and li tags 

        unset($download_ids_used_till_now); 
        unset($all_download_ids); 
       } else { //There are few more download ids 
        $download_ids_used_till_now[] = $download['download_id']; 

        echo '<li>'; 
        wdm_print_download_file_name($download, $product_meta); 
        echo '</li>'; 
       } 
      } 
      do_action('woocommerce_available_download_end', $download); 
      ?> 

     <?php endforeach; ?> 
    </ul> 

    <?php endif; ?> 

이 WooCommerce 2.1.12에서 테스트되는 파일에 다음 내용을 추가합니다.

+0

덕분에 이루어졌다. 실제로 질문이 조금 오래 되더라도 나 자신이 알아 내지 못했기 때문에 앞으로 답을 시험해 보겠습니다. – George

+0

오늘 테스트를 거쳤으며 원활하게 작동합니다. 고마워. – George

+0

제품의 추천 이미지도 추가 할 수 있습니까? 그렇다면 어떻게? – George

0

내가

변경 위의 코드는 너무 일찍 자식 목록을 닫습니다 듯

  if (isset($check_if_this_is_last_element[1]) && $download['download_id'] == $check_if_this_is_last_element[1]) 
+0

질문을 이해하지 못했다고 생각합니다. – George

1

에 선

  if (isset($check_if_this_is_last_element[0]) && $download['download_id'] == $check_if_this_is_last_element[0]) 

를 해결. 따라서 한 제품 아래에서 두 번의 다운로드가있는 경우

<ul class="digital-downloads"> 
    <li> 
    <span>Product 1</span> 
     <ul> 
     <li><a href="#">Product 1 File 1</a></li> 
     </ul> 
    </li> 
    <li><a href="#">Product 1 File 2</a></li> 
</ul> 

대신에 html 마크 업을 div로 변경했습니다. 이것은 의미 론적으로 정확하지는 않지만 작업이 완료되고 코드 작성이 더 간단합니다.

function wdm_print_download_file_name($download, $product_meta) { 
if (is_numeric($download['downloads_remaining'])) 
    echo apply_filters('woocommerce_available_download_count', '<span class="count">' . sprintf(_n('%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce'), $download['downloads_remaining']) . '</span> ', $download); 

echo apply_filters('woocommerce_available_download_link', '<a href="' . esc_url($download['download_url']) . '">' . $product_meta[$download['download_id']]['name'] . '</a>', $download); //Print file name 
} 

if ($downloads = WC()->customer->get_downloadable_products()) : 
?> 

<h2><?php echo apply_filters('woocommerce_my_account_my_downloads_title', __('Available Downloads and Videos', 'woocommerce')); ?></h2> 

<div class="digital-downloads"> 
    <?php 
    $all_product_ids = array(); 
    $all_download_ids = array(); 
    foreach ($downloads as $download) : 
     ?> 

     <?php 
     do_action('woocommerce_available_download_start', $download); 

     if (!in_array($download['product_id'], $all_product_ids)) { //Check if current product id is already there in $all_product_ids array. If it goes in this loop, that means it is new product id 
      $product_meta = get_post_meta($download['product_id'], '_downloadable_files', true); //All download ids of the product 

      $all_product_ids[] = $download['product_id']; //Push Current download's Product id to an array 

      $all_download_ids = array_keys($product_meta); //Get all download ids of the current product 

      echo '<div class="download-title">' . get_the_title($download['product_id']) . '</div>'; //Print product name 

     } else { 
      //product id is already in use. 
     } 

     echo '<div class="download-product">'; 
     wdm_print_download_file_name($download, $product_meta); 
     echo '</div>'; 

     do_action('woocommerce_available_download_end', $download); 
     ?> 

    <?php endforeach; ?> 
</div> 
<?php endif; ?> 

는 WooCommerce 2.5.2

+0

동일한 문제가 없습니다. 나는 항상 하나의 제품 아래에 적어도 두 개의 다운로드를 가지고 있으며 둘 다 두 번째 파일을 보여 주었던 것처럼 보입니다. – George