开发者

How do I create a custom currency type in Magento or Zend?

开发者 https://www.devze.com 2023-02-16 02:19 出处:网络
I\'m looking to create a new reward points currency, so instead of my Magento store selling products with a dollar value $300.00, I want it to display 300 Reward Points.

I'm looking to create a new reward points currency, so instead of my Magento store selling products with a dollar value $300.00, I want it to display 300 Reward Points.

I've already tried a bad practice solution by adding this to the currencies section in lib/Zend/Locale/Data/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>

I was able to enable and use this in Magento by following this thread: http://www.magentocommerce.com/boards/viewthread/56508/ but it still uses the default formatting pattern: ¤ #,##0.00 so it looks like Reward Points800.00

My locale is set to en_CA and as far as i can tell there's no way for me to change the formatting pattern without affecting the CDN and USD formatting as well.

I tried overriding Mage_Core_Model_Store so that if the current currency code is RWP it will format the price using an array of formatting options but this doesn't work when I'm in the product view. Not to mention that this also seems like a really dirty way to accomplish what I want.

 /**
 * 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'  => Posi开发者_运维技巧tion 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;
}


The currency system is one I'm only passingly familiar with, so take all this with a grain of salt. (also, assuming Magento 1.4.2)

One approach is the directory/currency model. This is class that all currency formatting functions and methods ultimately call. You'll see calls like this throughout the source code

Mage::getModel('directory/currency')

It doesn't look like there's a way to say "use this currency model/class for this currency", so you'll be stuck with a class rewrite here. The formatPrecision and formatTxt methods are the ones you're after.

Also, it looks like the directory/currency class wraps calls to Magento's locale object (calls to getNumber and currency)

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);
}

The locale object is a core/locale. You could also rewrite this class. if those were the methods you were after.

Finally, because this is Stack Overflow, there's a number of reward points systems already implemented for Magento. Checking these out to see how they solved the problems you're running into might be worthwhile.

0

精彩评论

暂无评论...
验证码 换一张
取 消