2014-03-28 37 views
0

在我的申請,我有我的產品免費門票和付費門票,用戶可以添加免費和付費門票。如果價格爲0,購物車類將無法添加產品。如果價格爲0,則需要在購物車中添加產品,但我不得不延長購物車類,但沒有任何結果。幫助讚賞。店0作爲價格在購物車codeigniter類

<?php 

    class MY_Cart extends CI_Cart 
    { 
     function __construct() 
     { 
      parent::__construct(); 
     } 



    function insert_item($items = array()) 
    { 
     // Was any cart data passed? No? Bah... 
     if (! is_array($items) OR count($items) == 0) 
     { 
      log_message('error', 'The insert method must be passed an array containing data.'); 
      return FALSE; 
     } 

     $save_cart = FALSE; 
     if (isset($items['id'])) 
     { 
      if (($rowid = $this->_insert_item($items))) 
      { 
       $save_cart = TRUE; 
      } 
     } 
     else 
     { 
      foreach ($items as $val) 
      { 
       if (is_array($val) AND isset($val['id'])) 
       { 
        if ($this->_insert_item($val)) 
        { 
         $save_cart = TRUE; 
        } 
       } 
      } 
     } 

     // Save the cart data if the insert was successful 
     if ($save_cart == TRUE) 
     { 
      $this->_save_cart(); 
      return isset($rowid) ? $rowid : TRUE; 
     } 

     return FALSE; 
    } 

    function _insert_item($items = array()) 
    { 
     // Was any cart data passed? No? Bah... 
     if (! is_array($items) OR count($items) == 0) 
     { 
      log_message('error', 'The insert method must be passed an array containing data.'); 
      return FALSE; 
     } 

     // -------------------------------------------------------------------- 

     // Does the $items array contain an id, quantity, price, and name? These are required 
     if (! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) 
     { 
      log_message('error', 'The cart array must contain a product ID, quantity, price, and name.'); 
      return FALSE; 
     } 

     // -------------------------------------------------------------------- 

     // Prep the quantity. It can only be a number. Duh... 
     $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty'])); 
     // Trim any leading zeros 
     $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty'])); 

     // If the quantity is zero or blank there's nothing for us to do 
     if (! is_numeric($items['qty']) OR $items['qty'] == 0) 
     { 
      return FALSE; 
     } 

     if (! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id'])) 
     { 
      log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores'); 
      return FALSE; 
     } 

     if (! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) 
     { 
      log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces'); 
      return FALSE; 
     } 

     // -------------------------------------------------------------------- 

     // Prep the price. Remove anything that isn't a number or decimal point. 
     $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price'])); 

     // Is the price a valid number? 
     if (! is_numeric($items['price'])) 
     { 
      log_message('error', 'An invalid price was submitted for product ID: '.$items['id']); 
      return FALSE; 
     } 

     if (isset($items['options']) AND count($items['options']) > 0) 
     { 
      $rowid = md5($items['id'].implode('', $items['options'])); 
     } 
     else 
     { 
      $rowid = md5($items['id']); 
     } 

     // -------------------------------------------------------------------- 

     // Now that we have our unique "row ID", we'll add our cart items to the master array 

     // let's unset this first, just to make sure our index contains only the data from this submission 
     unset($this->_cart_contents[$rowid]); 

     // Create a new index with our new row ID 
     $this->_cart_contents[$rowid]['rowid'] = $rowid; 

     // And add the new items to the cart array 
     foreach ($items as $key => $val) 
     { 
      $this->_cart_contents[$rowid][$key] = $val; 
     } 

     // Woot! 
     return $rowid; 
    } 
+0

將0轉換爲數字格式可以解決您的問題嗎? –

+0

如何將0轉換爲數字格式?幫助讚賞。 – Chopra

+0

number_format($ priceVariable,2,'。',',') –

回答

0

變化數number_format()

number_format($variable, 2, '.', ',') 

也是爲什麼你使用的preg_match檢查數字爲價格的格式有很多功能在PHP這個檢查數量一樣嘗試

is_numeric($items['price']) 
+0

它將值0轉換爲.00。它也修剪領先的0.沒有運氣。 – Chopra

+0

試試echo number_format(0,2,'。',','); //0.00可能是你有什麼問題,請逐行調試價格 –

+0

基本上購物車功能轉換爲0.00到.00。然後,我把.00存儲在數據庫中。但在閱讀或更新時,它不會輸入新值。有一些問題。 – Chopra

0

您可以對此發表評論198

$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));