我试图在查看订单表中显示每个单个产品的标签或标签。这是我的代码在override review-order.php,但不工作。From my review-order.php:
<tbody>
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
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 );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) ) . ' '; ?>
</td>
<td>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( 'nº %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
<td>
<?php
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
if( in_array( 'bird', $term_array ) ) {
echo 'Bird';
}
elseif( in_array( 'dog', $term_array ) ) {
echo 'Dog';
}
else {
echo 'not exists animal';
}
?>
</td>
</tr>
<?php
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
?>
</tbody>
只返回"not exists animal"。显然出了什么问题!但我不知道是什么。更好的是工作在自定义woocommerce-functions.php我有,但再一次我不知道如何。谢谢你的帮助!
解决!基于这个问题及其答案:在购物车和结帐页面上显示Woocommerce产品类别看看没有投票的答案,并使用一点想象力,下面是工作代码。为了完成测试,除了标签之外,我还添加了产品类别。我希望能对别人有所帮助。这是我现在重写的order-review.php模板。
<?php
/**
* Review order table
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/review-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerceTemplates
* @version 5.2.0
*/
defined( 'ABSPATH' ) || exit;
?>
<table class="shop_table woocommerce-checkout-review-order-table">
<thead>
<tr>
<th class="product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
<th class="product-name"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
<th class="product-name"><?php esc_html_e( 'Tag', 'woocommerce' ); ?></th>
<th class="product-name"><?php esc_html_e( 'Category', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php
do_action( 'woocommerce_review_order_before_cart_contents' );
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 );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-name">
<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) ) . ' '; ?>
</td>
<td>
<?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( 'nº %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</td>
<td>
<?php
$terms = get_the_terms( $product_id, 'product_tag' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ($terms as $term) {
$product_tag = $term->name;
}
echo $product_tag ;
}
?>
</td>
<td>
<?php
$terms = get_the_terms( $product_id, 'product_cat' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ($terms as $term) {
$product_cat = $term->name;
}
echo $product_cat ;
}
?>
</td>
</tr>
<?php
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
?>
</tbody>
</table>
请注意:
我添加了这个代码(来自cart.php)
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
和this in the loop
<td> <?php $terms = get_the_terms( $product_id, 'product_tag' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ($terms as $term) { $product_tag = $term->name; } echo $product_tag ; } ?> </td> <td> <?php $terms = get_the_terms( $product_id, 'product_cat' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ($terms as $term) { $product_cat = $term->name; } echo $product_cat ; } ?> </td>
在这里!