【问题标题】:How To Remove Decimal From Magento-1 Prices?如何从 Magento-1 价格中删除小数点?
【发布时间】:2014-12-04 07:52:49
【问题描述】:

我在搜索中找到的只是一个编程解决方案。 我知道我们可以为英文商店修改 /lib/Zend/Locale/Data/en.xml。 en.xml 中有这部分:

       <currencyFormats>
            <currencyFormatLength>
                <currencyFormat>
                    <pattern>#,##0.00 ¤</pattern>
                </currencyFormat>
            </currencyFormatLength>
        </currencyFormats>

价格以这种格式显示:1,321.54 现在要从价格中删除小数部分,我认为我唯一要做的就是将 en.xml 更改为如下所示:

<currencyFormats>
            <currencyFormatLength>
                <currencyFormat>
                    <pattern>#,##0 ¤</pattern>
                </currencyFormat>
            </currencyFormatLength>
        </currencyFormats>

问题是在此更改后价格显示为所需的(1,132 格式),但没有货币符号 ($)。 我在这里缺少什么? 提前致谢。


更新 我还在尝试,当 pattern 节点更改为以下

<pattern>¤ #,##0</pattern>

价格带有货币符号($ 1,132)但不在所需位置O_O,要求货币符号在右侧没有左侧:( SO..

【问题讨论】:

标签: php xml magento


【解决方案1】:

为了更改 magento 中的价格精度,您需要覆盖一些核心文件。

在下面的示例中,我们将精度更改为 0。

1) 覆盖 lib/Zend/Currency.php 并更改行周围的精度:

 69     protected $_options = array(
 70         'position'  => self::STANDARD,
 71         'script'    => null,
 72         'format'    => null,
 73         'display'   => self::NO_SYMBOL,
 74         'precision' => 0,    /*CHANGE*/
 75         'name'      => null,
 76         'currency'  => null,
 77         'symbol'    => null,
 78         'locale'    => null,
 79         'value'     => 0,
 80         'service'   => null,
 81         'tag'       => 'Zend_Locale'
 82     );

2)覆盖app/code/core/Mage/Core/Model/Store.php并改变roundPrice函数:

public function roundPrice($price)
{    
    return round($price, 4);
}

3)覆盖app/code/core/Mage/Directory/Model/Currency.php并改变格式函数:

public function format($price,
                            $options=array(),
                            $includeContainer = true,
                            $addBrackets = false)
{   
  return $this->formatPrecision( $price,
                                      4,
                                      $options,
                                      $includeContainer,
                                      $addBrackets);
}

【讨论】:

  • 谢谢,我认为这是唯一的方法.. 但是我可以确保所有价格都在视图中和所有结帐过程中都是 ceil 价值吗??
  • 你必须使用 0 而不是 4,否则将有 4 个十进制数字。
【解决方案2】:

这里的所有答案都涉及更改核心文件。这是任何人都应该做的。要么开发一个模块并进行这些更改,要么像这样保留核心文件并使用str_replace 更改价格。

所以进入 theme/template/catalog/product/price.phtml 并(取决于配置)在第 209 行左右更改:

$_coreHelper->formatPrice($_price, true)

进入

$without_decimals = $_coreHelper->formatPrice($_price, true); echo str_replace(".00", "", $without_decimals); 

这会从价格中删除 .00。好处是保留了其他小数的价格。如果您不希望这样,您可以删除点后的所有内容并使用round() 函数将数字向上取整。

根据配置,其他价格可能需要更改(如果您显示不含税的价格等)

【讨论】:

  • 我认为这有点不对,因为它不仅仅是查看没有小数的价格,如果我按照你的方式进行,magento 会在没有四舍五入的情况下下单,前端将是四舍五入的价格。不是吗?
  • @rramiii 如果您的值不是 0 作为小数,您还必须在 ajax 添加到购物车中实施此更改,以便在结帐时这些价格保持不变。但我不明白为什么有人会插入价格为 56.89 并希望它在前端显示为 57。只需在后端将其设置为 57。
  • 这不起作用有几个原因。 1. 它只适用于以 .00 结尾的价格。因此,当产品打折并且您获得价格的十进制值时,它将不起作用。 2. 不适用于可配置产品。虽然价格来自帮助程序,但它们被 javascript 替换为 JSON 数组 Product.OptionsPrice 的内容,该数组是在 getJsonConfig 中创建的。
  • 覆盖核心文件是对的,但在 FR 中,它们使用格式 , 而不是 .
【解决方案3】:

要从价格中删除小数部分,您需要修改文件 "code/core/Mage/Directory/Model/Currency.php"

首先,而不是行:

return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);

使用:

return $this->formatPrecision($price, 0, $options, $includeContainer, $addBrackets);

要更改货币符号的位置,请使用以下行修改文件“lib/Zend/Locale/Data/en.xml”:

<pattern>#,##0.00 ¤;(#,##0.00 ¤)</pattern>

完成后,不要忘记清除缓存。

附:为避免升级过程中出现任何问题,我们强烈建议您通过扩展实现上述所有更改: (在这里查看工具:http://www.magentocommerce.com/magento-connect/catalogsearch/result/?id=&s=7&pl=0&eb=0&hp=0&q=currency|position&t=1&p=1

【讨论】:

    【解决方案4】:

    要去掉价格的小数部分,需要修改文件:

    1) 首先是改变线周围的精度

    lib/Zend/Currency.php

    protected $_options = array(
        'position'  => self::STANDARD,
        'script'    => null,
        'format'    => null,
        'display'   => self::NO_SYMBOL,
        'precision' => 2,
        'name'      => null,
        'currency'  => null,
        'symbol'    => null,
        'locale'    => null,
        'value'     => 0,
        'service'   => null,
        'tag'       => 'Zend_Locale'
    );
    

    更改= 'precision' => 2, 到 'precision' => 0,

    2) 第二个文件(更改roundPrice函数:)

    app/code/core/Mage/Core/Model/Store.php

    public function roundPrice($price)
    {
        return round($price, 2);
    }
    

    收件人

    public function roundPrice($price)
    {
        return round($price, 0);
    }
    

    3) 第三个也是最后一个是改变格式功能:

    app/code/core/Mage/Directory/Model/Currency.php

    public function format($price,
                            $options=array(),
                            $includeContainer = true,
                            $addBrackets = false)
    {   
     return $this->formatPrecision( $price,
                                      2,
                                      $options,
                                      $includeContainer,
                                      $addBrackets);
    }
    

    public function format($price,
                            $options=array(),
                            $includeContainer = true,
                            $addBrackets = false)
     {   
     return $this->formatPrecision( $price,
                                      0,
                                      $options,
                                      $includeContainer,
                                      $addBrackets);
     }
    

    【讨论】:

      【解决方案5】:

      除了以上所有内容之外,您还可以再做一项更改。

      请到PriceCurrency.php页面 然后将最后一行更改为

      public function round($price)
      {
          return round($price, 0);
      }
      

      之后在PriceCurrencyInterface.php页面制作

      const DEFAULT_PRECISION = 0;
      

      在顶部。

      就是这样。希望它会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 2015-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多