I'm trying to add product category to the email notification. The custom fields (Time and Date) work but the product category is displaying as an "Array".
function render_product_description($item_id, $item, $order){
$_product = $order->get_product_from_item( $item );
echo "<br>" . $_product->post->post_content;
echo "<br>";
echo '<p><strong>Time:</strong><br />';
echo get_post_meta($_product->id, 'time', true) .'</p>';
echo '<p><strong>Category:</strong><br />';
echo wp_get_post_terms($_product->id, 'product_cat', true) .'</p>';
echo '<p><strong>Date:</strong><br />';
echo get_post_meta($_product->id, 'date', true) .'</p>';
}
add_action('woocommerce_order_item_meta_end', 'render_product_description',10,3);
You can use
implode()
function that join array values in a string.
I also added an extra check for empty values
function render_product_description( $item_id, $item, $order, $plain_text ) {
// Get product id
$product_id = $item->get_product_id();
// Get product
$product = $item->get_product();
// Product content
$product_content = $product->post->post_content;
// NOT empty
if ( ! empty ( $product_content ) ) {
echo '<p>' . $product_content . '</p>';
}
// Get post meta
$time = get_post_meta( $product_id, 'time', true );
// NOT empty
if ( ! empty ( $time ) ) {
echo '<p><strong>Date:</strong><br />' . $time . '</p>';
}
// Get terms
$term_names = wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );
// NOT empty
if ( ! empty ( $term_names ) ) {
echo '<p><strong>Categories:</strong><br />' . implode( ", ", $term_names ) . '</p>';
}
// Get post meta
$date = get_post_meta( $product_id, 'date', true );
// NOT empty
if ( ! empty ( $date ) ) {
echo '<p><strong>Date:</strong><br />' . $date . '</p>';
}
}
add_action( 'woocommerce_order_item_meta_end', 'render_product_description', 10, 4 );