我想重寫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
聲明,但我不知道如何得到它的設置。
我跟着你的代碼,但它沒有爲我工作。逗號仍然存在。它是你編輯的product-attributes.php文件嗎?是否有多個文件需要編輯?我在本地主機上運行我的WP,不知道這是否有所作爲。 – abracassabra
是的,它是product-attributes.php文件。我將文件從woocommerce插件文件夾複製到wp-content/themes/mytheme/woocommerce/single-product/product-attributes.php,並在那裏進行了更改。這種方式升級友好。在localhost上運行應該沒有什麼區別。 –
好吧,我想通了,我使用的主題(主題融合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