我試圖修改僞購物車(基於用戶的元數據,而不是數據庫中存儲的值它的陣列)PHP文件中錯誤地顯示貨幣> 1000爲0.01顯示貨幣值大於1000
這裏是塊我想改變:
public function get_cart_value()
{
$cart = $this->cart;
$prices = array();
$cart = apply_filters('ss_mod_cart_value', $cart);
foreach ($cart['products'] as $product)
{
array_push($prices, array('price'=>trim($product['price']), 'discount' => trim($product['discount'])));
}
$total = 0;
foreach($prices as $price)
{
$price = $this->calc_discount($price['price'], $price['discount']);
$price = str_replace('.', '', $price['final_price']);
$total += $price;
}
$total = number_format($total/100, 2);
return $total;
}
的購物車按鈕,它的工作原理上正確顯示的項目不超過4個位數的總價值,例如:
300 + 500 = 800
但
300 + 500 + 1000 = 800.01
而不是1,800
我一直在試圖改變number_format()
,使其工作,但無法找到一個解決方案。
或許有點貧民窟建議,但你有沒有試過類似'(10 * 100)'? – Helpful
您的整個'foreach'循環看起來很可疑。您將覆蓋'$ price'的值,然後使用該結果進行下一次計算,但讓我感到困惑的是取代小數點。這應該是基本的算術運算,但看起來你正在做一些奇怪的字符串轉換,這會破壞你的結果。 –
嘗試http://stackoverflow.com/questions/5139793/php-unformat-money這個解決方案 –