如何在 Magento 或 Zend 中创建自定义货币类型?
我希望创建一种新的奖励积分货币,因此我希望它显示 300 奖励积分,而不是我的 Magento 商店销售美元价值 300.00 美元的产品。
我已经尝试过一种不好的实践解决方案,将其添加到 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>
我可以通过以下线程在 Magento 中启用和使用它: http://www.magentocommerce.com/boards/viewthread/56508/ 但它仍然使用默认格式模式:¤ #,##0.00
,因此看起来像 Reward 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;
}
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' => 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对货币体系只是略知一二,所以对这一切持保留态度。 (另外,假设 Magento 1.4.2)
一种方法是目录/货币模型。这是所有货币格式化函数和方法最终调用的类。您将在整个源代码中看到这样的调用
,看起来没有办法说“对此货币使用此货币模型/类”,因此您将在此处进行类重写。
formatPrecision
和formatTxt
方法是您需要的方法。另外,看起来
directory/currency
类包装了对 Magento 语言环境对象的调用(调用getNumber
和currency
)。语言环境对象是一个 <代码>核心/语言环境。您也可以重写这个类。如果这些是您所追求的方法。
最后,因为这是 Stack Overflow,所以 Magento 已经实现了许多奖励积分系统。检查这些内容以了解它们如何解决您遇到的问题可能是值得的。
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 codeIt 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
andformatTxt
methods are the ones you're after.Also, it looks like the
directory/currency
class wraps calls to Magento's locale object (calls togetNumber
andcurrency
)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.