![](/img/trans.png)
[英]Get Cart products id on checkout WooCommerce page, to display product images
[英]Get the product categories and display it on cart page woocommerce
我正在嘗試在購物車頁面上顯示每種產品的類別。 我對php的了解不多。 我正在使用此代碼:
$terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
}
echo $product_cat ;
為了測試這個代碼,我把一個<div>
在woocommerce/cart/cart.php
剛下foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
循環和它的工作,但它僅顯示每個產品的最后一個類別,而不顯示所有類別。
我想顯示所有類別,而不是在頁面的開頭,而是在運送信息之后,小計和總計之前。
我試圖將這段代碼放在新的<tr><th>Some Title</th><td>[PHP Code here]</td></tr>
中的cart-totals.php
中,但沒有顯示任何內容結果。 我認為這是因為代碼從foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
消失了foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
。 任何幫助,將不勝感激,謝謝
[編輯]
我不知道這是否是最好的方法,但是此代碼似乎可以在cart-total.php
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$terms = get_the_terms( $product_id, 'product_cat' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);
?>
謝謝 :)
試試看
$terms = get_the_terms( $product_id, 'product_cat' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);
這會將所有類別放置在一個數組中,您可以將其內爆以字符串形式輸出,並用逗號分隔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.