2011-03-11 50 views
5

我期待創造一個新的獎勵積分貨幣,所以我想要它顯示300獎勵積分,而不是我的Magento商店銷售價值300美元的產品。如何在Magento或Zend中創建自定義貨幣類型?

我已經加入這在LIB貨幣節嘗試了不好的做法,解決方案/的Zend /區域設置/數據/ en.xml

<currency type="RWP"> 
      <displayName>Reward Point</displayName> 
      <displayName count="one">Reward Point</displayName> 
      <displayName count="other">Reward Points</displayName> 
      <symbol>Reward Points</symbol> 
</currency> 

我能夠啓用和使用在Magento下面這個線程: http://www.magentocommerce.com/boards/viewthread/56508/ 但它仍然使用默認的格式設置模式:¤ #,##0.00所以它看起來像獎勵Points800.00

我的區域設置爲en_CA,據我所知,沒有辦法改變格式化格式,同時不影響CDN和USD格式。

我試着覆蓋Mage_Core_Model_Store,這樣如果當前的貨幣代碼是RWP,它將使用一組格式化選項格式化價格,但是當我處於產品視圖時,這不起作用。更何況,這似乎是一個非常骯髒的方式來完成我想要的。

/** 
* Format price with currency filter (taking rate into consideration) 
* 
* @param double $price 
* @param bool $includeContainer 
* @return string 
*/ 
public function formatPrice($price, $includeContainer = true) 
{ 
    if ($this->getCurrentCurrency()) { 
     /** 
     * Options array 
     * 
     * The following options are available 
     * 'position' => Position for the currency sign 
     * 'script' => Script for the output 
     * 'format' => Locale for numeric output 
     * 'display' => Currency detail to show 
     * 'precision' => Precision for the currency 
     * 'name'  => Name for this currency 
     * 'currency' => 3 lettered international abbreviation 
     * 'symbol' => Currency symbol 
     */ 
     $options = array(); 

     if ($this->getCurrentCurrencyCode() == 'RWP') { 
      $options = array(
       'position' => 16, 
       'precision' => 0, 
       'format'=> '#,##0.00 ' 
      ); 
     } 
     return $this->getCurrentCurrency()->format($price, $options, $includeContainer); 
    } 
    return $price; 
} 
+0

你有沒有遇到過這種情況?我正在Magento 2中做同樣的事情,並且增加一個新貨幣似乎是一項不重要的任務。 – floorz

+0

是的,我相信我做到了,但那是6年前的事情,我幾乎沒有碰過PHP,更不用說Magento的時間差不多了。對不起:( –

回答

0

貨幣系統是我只是通常熟悉的一個,所以把所有這一切都用一粒鹽。 (另外,假設Magento 1.4.2)

一種方法是directory/currency模型。這是所有貨幣格式化函數和方法最終調用的類。你會看到類似這樣的電話在整個源代碼

Mage::getModel('directory/currency') 

它看起來並不像有辦法說「使用此貨幣模型/類此貨幣」,所以你會用一個類被卡住在這裏重寫。 formatPrecisionformatTxt方法是你所追求的方法。

此外,它看起來像directory/currency類包裝調用Magento的Locale對象(調用getNumbercurrency

public function formatTxt($price, $options=array()) 
{ 
    if (!is_numeric($price)) { 
     $price = Mage::app()->getLocale()->getNumber($price); 
    } 
    /** 
    * Fix problem with 12 000 000, 1 200 000 
    * 
    * %f - the argument is treated as a float, and presented as a floating-point number (locale aware). 
    * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). 
    */ 
    $price = sprintf("%F", $price); 
    return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options); 
} 

語言環境對象是core/locale。你也可以重寫這個類。如果那些是你以後的方法。

最後,由於這是堆棧溢出,因此已經爲Magento實施了許多獎勵積分系統。檢查這些以瞭解他們如何解決您遇到的問題可能是值得的。