2013-07-03 26 views
1

我想重寫Woocommerce輸出屬性的方式。默認情況下,屬性顯示在兩列中 - 第1列是標籤,第2列是以逗號分隔的屬性列表。我想覆蓋它並讓每個屬性顯示在它自己的單元格中。這裏是原代碼:Woocommerce從逗號變爲表格單元格

<?php foreach ($attributes as $attribute) : 

    if (empty($attribute['is_visible']) || ($attribute['is_taxonomy'] && ! taxonomy_exists($attribute['name']))) 
     continue; 
    ?> 

    <tr class="<?php if (($alt = $alt * -1) == 1) echo 'alt'; ?>"> 
     <th><?php echo $woocommerce->get_helper('attribute')->attribute_label($attribute['name']); ?></th> 

     <td><?php 
      if ($attribute['is_taxonomy']) { 

       $values = woocommerce_get_product_terms($product->id, $attribute['name'], 'names'); 
       echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

      } else { 

       // Convert pipes to commas and display values 
       $values = array_map('trim', explode('|', $attribute['value'])); 
       echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

      } 
     ?></td> 
     <?php endforeach; ?> 
    </tr> 

<?php endforeach; ?> 

我一直在想我需要設置td內的另一foreach聲明,但我不知道如何得到它的設置。

回答

1

Basicly你有,與該代碼,此輸出:

<tr> 
<th>atribute label</th> 
<td>atribute name</td> 
<tr> 

所以,你只需要輸出類似:「屬性附加傷害標籤 - 屬性附加傷害名」,爲您只需要刪除次標籤並將標籤的代碼放入td標籤中。

成才這樣的:

<tr class="<?php if (($alt = $alt * -1) == 1) echo 'alt'; ?>"> 


    <td> 
     <?php echo $woocommerce->get_helper('attribute')->attribute_label($attribute['name']); ?> 
     <?php 
     if ($attribute['is_taxonomy']) { 

      $values = woocommerce_get_product_terms($product->id, $attribute['name'], 'names'); 
      echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

     } else { 

      // Convert pipes to commas and display values 
      $values = array_map('trim', explode('|', $attribute['value'])); 
      echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); 

     } 
    ?></td> 
    <?php endforeach; ?> 
</tr> 
1

我想通了......我需要調整的foreach語句和細胞是如何出現的行(顯示內容在其中),這是我完成的代碼這是工作,但是,我很想改進它的建議:

<?php foreach ($attributes as $attribute) : 

    if (empty($attribute['is_visible']) || ($attribute['is_taxonomy'] && ! taxonomy_exists($attribute['name']))) 
     continue; 
    ?> 
    <tr> 
     <th scope="row"><?php echo $woocommerce->attribute_label($attribute['name']); ?></th> 
     <?php 
      if ($attribute['is_taxonomy']) { 

       $values = woocommerce_get_product_terms($product->id, $attribute['name'], 'names'); 
       foreach ($values as $value) : 
        echo '<td>'; 
        echo $value; 
        echo '</td>'; 
       endforeach; 
      } else { 

       $values = array_map('trim', explode('|', $attribute['value'])); 
       foreach ($values as $value) : 
        echo '<td>'; 
        echo $value; 
        echo '</td>'; 
       endforeach; 
      } 
     ?> 
    </tr><?php endforeach; ?> 
+0

我跟着你的代碼,但它沒有爲我工作。逗號仍然存在。它是你編輯的product-attributes.php文件嗎?是否有多個文件需要編輯?我在本地主機上運行我的WP,不知道這是否有所作爲。 – abracassabra

+1

是的,它是product-attributes.php文件。我將文件從woocommerce插件文件夾複製到wp-content/themes/mytheme/woocommerce/single-product/product-attributes.php,並在那裏進行了更改。這種方式升級友好。在localhost上運行應該沒有什麼區別。 –

+0

好吧,我想通了,我使用的主題(主題融合Avada)似乎將Woocommerce文件保存在2個目錄中,如您所說,可能會升級友好;所以我已經有了2個文件副本,第一個路徑是wp-content/plugins/woocommerce/templates/single-product/product-attributes.php,第二個路徑是wp-content/themes/Avada/woocommerce/single-product /product-attributes.php。原來在wp-content/plugins/woocommerce /下沒有做任何事情編輯這個文件,不得不改變wp-content/themes/Avada/woocommerce /。之後工作:) – abracassabra