2014-04-10 81 views
0

我試圖修改僞購物車(基於用戶的元數據,而不是數據庫中存儲的值它的陣列)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(),使其工作,但無法找到一個解決方案。

+0

或許有點貧民窟建議,但你有沒有試過類似'(10 * 100)'? – Helpful

+1

您的整個'foreach'循環看起來很可疑。您將覆蓋'$ price'的值,然後使用該結果進行下一次計算,但讓我感到困惑的是取代小數點。這應該是基本的算術運算,但看起來你正在做一些奇怪的字符串轉換,這會破壞你的結果。 –

+0

嘗試http://stackoverflow.com/questions/5139793/php-unformat-money這個解決方案 –

回答

0

我認爲通過此線

$price = str_replace('.', '', $price['final_price']); 

300 + 500 + 「1000」;

你的號碼,如1000被轉換成字符串,然後你的總變成801

你必須轉換成浮在適當的方式在本

PHP: unformat money

+0

我跟隨了線程,發現了一個解決方法:用$ price = floatval替換str_replace和floatval(preg_replace('/ [^ \ d \'] /','',$ price ['final_price']));並將總數更改爲$ total = number_format($ total,2);現在貨幣顯示正確。非常感謝你! – hanasuz