显示 WooCommerce 中购物车项目的特定最高自定义字段值

我正在 WooCommerce 中使用高级自定义字段插件,并且我的 WooCommerce 产品有一个自定义字段get_post_meta( get_the_ID(), "lead_time", true );“。当有人结帐时,此字段会显示每个产品的交货时间(如果缺货)。


我需要从所有购物车商品/订单商品中找到最高的“交货时间”,然后将该数字显示为订单上的最终交货时间。


以下代码显示购物车中所有产品的交货时间:


foreach ( WC()->cart->get_cart() as $cart_item ) {

    $leadTimes = get_post_meta($cart_item['product_id'] , 'lead_time', true );

    echo $leadTimes;

}

例如,购物车/订单中有 3 个产品:


第一个有 7 天的交货时间,

第二个的交货时间为 14 天,

第三个的交货时间为 7 天。

但它显示 7147。


我需要显示“交货时间 = 14 天”,因为 14 是购物车中 3 件商品的最长交货时间。我已经尝试了所有我能想到的使用上面的 foreach 循环的可能组合,已经 3 天了。有很多不同的结果,但不是我需要的结果。


任何帮助将不胜感激。


慕仙森
浏览 76回答 1
1回答

吃鸡游戏

使用 php 函数尝试以下操作max():$lead_time = array(); // Initializing// Loop through cart itemsforeach ( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; $lead_time[] = get_post_meta( $cart_item['product_id'], 'lead_time', true);}echo '<p>Max Lead time: ' . max($lead_time) . ' days</p>';您将获得最大值(显示的)。或者当您使用高级自定义字段时,这也应该有效:$lead_time = array(); // Initializing// Loop through cart itemsforeach ( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; $lead_time[] = get_field('lead_time', $cart_item['product_id']);}echo '<p>Max Lead time: ' . max($lead_time) . ' days</p>';
打开App,查看更多内容
随时随地看视频慕课网APP