我正在 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 天了。有很多不同的結果,但不是我需要的結果。任何幫助將不勝感激。
1 回答

吃雞游戲
TA貢獻1829條經驗 獲得超7個贊
使用 php 函數嘗試以下操作max():
$lead_time = array(); // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$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 items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$lead_time[] = get_field('lead_time', $cart_item['product_id']);
}
echo '<p>Max Lead time: ' . max($lead_time) . ' days</p>';
- 1 回答
- 0 關注
- 98 瀏覽
添加回答
舉報
0/150
提交
取消