【问题标题】:Get the last item in an array from Order Notes in Woocommerce从 Woocommerce 中的 Order Notes 获取数组中的最后一项
【发布时间】:2019-06-27 00:35:47
【问题描述】:

我想在发送给客户的电子邮件中包含最后的订单备注。目前它输出所有订单备注,我如何编辑此 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;
?>

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

【问题讨论】:

    标签: php wordpress foreach php-7


    【解决方案1】:

    你可以去掉 foreach 语句,像这样使用end() 函数:

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

    【讨论】:

      猜你喜欢
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      相关资源
      最近更新 更多