【问题标题】:Magento how to strip decimal from price for one storeMagento如何从一家商店的价格中去除小数
【发布时间】:2013-07-28 22:53:03
【问题描述】:

有一些资源介绍了如何在 Magento 或整个网站中去除某种货币的小数点,但没有介绍如何为某个商店做到这一点。

背景:

每种货币的价格格式在 /lib/Zend/Currency.php (http://mrtony.org/2013/01/removing-decimals-in-currency-for-magento/) 中进行管理,因此您可以使用数字格式覆盖那里的货币小数。此文件不可覆盖,其中的更改将影响整个站点。

如果您想从核心向前迈出一步,更多地进入可覆盖的显示代码,您可以使用 code/core/Mage/Directory/Model/Currency.php (http://magentocoders.blogspot.com.au/2011/10/how-to-remove-decimal-price-in-magento.html)

如果您只想为一家商店做上述两种情况,该怎么办?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    试试这个:

    app/etc/modules/Madison_Overrides.xml

    <?xml version="1.0"?>
    <config>
         <modules>
            <Madison_Overrides>
                <active>true</active>
                <codePool>local</codePool>
            </Madison_Overrides>
         </modules>
    </config>
    

    app/code/local/Madison/Overrides/etc/config.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Madison_Overrides>
          <version>1.0</version>
        </Madison_Overrides>
      </modules>
      <global>
        <models>
            <directory>
              <rewrite>
                  <currency>Madison_Overrides_Model_Currency</currency>
              </rewrite>
            </directory>
            <core>
              <rewrite>
                  <locale>Madison_Overrides_Model_Locale</locale>
              </rewrite>
            </core>
        </models>
      </global>
    </config> 
    

    app/code/local/Madison/Overrides/Model/Currency.php

    <?php
    
    class Madison_Overrides_Model_Currency extends Mage_Directory_Model_Currency
    {
        /**
         * Format price to currency format
         *
         * @param   double $price
         * @param   bool $includeContainer
         * @return  string
         */
        public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
        {
            //get the store id so you an correctly reference the global variable 
            $store_id = Mage::app()->getStore()->getId();
            //JASE get precision from custom variable that can be set at store level
            $getPrecision = Mage::getModel('core/variable')->setStoreId($store_id)->loadByCode('decimalPrecision')->getData('store_plain_value'); 
            //Mage::log("Precision is ".$getPrecision,null,'jase.log');
            //if set use it, otherwise default to two decimals
            $precision = is_numeric($getPrecision) ? $getPrecision : 2 ; 
            return $this->formatPrecision($price, $precision, $options, $includeContainer, $addBrackets);
        }
    }
    
    ?>
    

    app/code/local/Madison/Overrides/Model/Locale.php

    <?php
    
    class Madison_Overrides_Model_Locale extends Mage_Core_Model_Locale
    {
        /**
         * Functions returns array with price formatting info for js function
         * formatCurrency in js/varien/js.js
         *
         * @return array
         */
        public function getJsPriceFormat()
        {
            $format = Zend_Locale_Data::getContent($this->getLocaleCode(), 'currencynumber');
            $symbols = Zend_Locale_Data::getList($this->getLocaleCode(), 'symbols');
    
            $pos = strpos($format, ';');
            if ($pos !== false){
                $format = substr($format, 0, $pos);
            }
            $format = preg_replace("/[^0\#\.,]/", "", $format);
            $totalPrecision = 0;
            $decimalPoint = strpos($format, '.');
            if ($decimalPoint !== false) {
                $totalPrecision = (strlen($format) - (strrpos($format, '.')+1));
            } else {
                $decimalPoint = strlen($format);
            }
            $requiredPrecision = $totalPrecision;
            $t = substr($format, $decimalPoint);
            $pos = strpos($t, '#');
            if ($pos !== false){
                $requiredPrecision = strlen($t) - $pos - $totalPrecision;
            }
            $group = 0;
            if (strrpos($format, ',') !== false) {
                $group = ($decimalPoint - strrpos($format, ',') - 1);
            } else {
                $group = strrpos($format, '.');
            }
            $integerRequired = (strpos($format, '.') - strpos($format, '0'));
    
            //get the store id so you an correctly reference the global variable 
            $store_id = Mage::app()->getStore()->getId();
            //JASE get precision from custom variable that can be set at store level
            $getPrecision = Mage::getModel('core/variable')->setStoreId($store_id)->loadByCode('decimalPrecision')->getData('store_plain_value'); 
            //if set use it, otherwise default to two decimals
            $totalPrecision = is_numeric($getPrecision) ? $getPrecision : $totalPrecision ; 
            $requiredPrecision = is_numeric($getPrecision) ? $getPrecision : $requiredPrecision ; 
    
            $result = array(
                'pattern' => Mage::app()->getStore()->getCurrentCurrency()->getOutputFormat(),
                'precision' => $totalPrecision,
                'requiredPrecision' => $requiredPrecision,
                'decimalSymbol' => $symbols['decimal'],
                'groupSymbol' => $symbols['group'],
                'groupLength' => $group,
                'integerRequired' => $integerRequired
            );
    
            return $result;
        }
    
    }
    
    ?>
    

    上面的第一个 php 文件覆盖了 php 中的小数点精度,上面的第二个 php 覆盖了 javascript 中的小数点精度,这是可配置产品或带有选项的产品所必需的。

    最后一步是您可以使用 Magento 中的自定义变量来控制每个商店的小数位数。尝试设置一个名为“decimalPrecision”的自定义变量。将“Plain Value”保存为 2。然后保存并返回。将“商店视图”更改为您的特定商店,并将“使用默认变量值”设置为“否”。确保在“Variable HTML Value”中放置一些文本以便保存(没关系)。在“Variable Plain Value”中输入数字“0”。现在使用此代码和该自定义变量,您选择的商店将没有小数,但其他地方将默认为 2 位小数。

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多