首页 > 解决方案 > 从 Woocommerce 中的 Order Notes 获取数组中的最后一项

问题描述

我想在发送给客户的电子邮件中包含最后的订单备注。目前它输出所有订单备注,我如何编辑此 foreach 以仅显示最后/最新订单备注?

安装使用 PHP 7.2,但理想情况下,我希望它适用于所有版本,我是否应该退回到我之前在测试站点上使用过的 PHP 5.6

<h2><?php _e( 'Order Notes', 'woocommerce' ); ?></h2>

<?php
$args = array(
    'status' => 'approve',
    'post_id' => $order->id
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo $comment->comment_content . '<br />';
endforeach;
?>

因此,期望的结果将显示“订单状态从暂停更改为已完成”

在此处输入图像描述

标签: phpwordpressforeachphp-7

解决方案


您可以摆脱 foreach 语句并使用如下end()函数:

$lastcomment = end($comments);
echo $lastcomment->comment_content . '<br />';

推荐阅读