Skip to content

Commit b439e2f

Browse files
sreichelkiatngCopilot
authored
[FEAT] allow price rounding between 0 and 4 digits (OpenMage#4701)
* [FEAT] allow price rounding between 0 and 4 digits * Update app/code/core/Mage/Catalog/Helper/Price.php * Update app/code/core/Mage/Catalog/Helper/Price.php * Update app/code/core/Mage/Catalog/etc/system.xml * Update app/code/core/Mage/Catalog/Helper/Price.php Co-authored-by: Copilot <[email protected]> * Update app/code/core/Mage/Catalog/Helper/Price.php Co-authored-by: Copilot <[email protected]> * Update app/code/core/Mage/Core/Model/Store.php Co-authored-by: Copilot <[email protected]> * Update app/code/core/Mage/Adminhtml/Block/Catalog/Product/Helper/Form/Price.php Co-authored-by: Copilot <[email protected]> * declare strict, rector, copyright --------- Co-authored-by: Ng Kiat Siong <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent e01ad16 commit b439e2f

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Helper/Form/Price.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function getEscapedValue($index = null)
7979
return null;
8080
}
8181

82-
return number_format((float) $value, 2, null, '');
82+
/** @var Mage_Catalog_Helper_Price $helper */
83+
$helper = Mage::helper('catalog/price');
84+
85+
return number_format((float) $value, $helper->getRoundingPrecision(), null, '');
8386
}
8487
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright For copyright and license information, read the COPYING.txt file.
7+
* @link /COPYING.txt
8+
* @license Open Software License (OSL 3.0)
9+
* @package Mage_Catalog
10+
*/
11+
12+
/**
13+
* Catalog price helper
14+
*
15+
* @category Mage
16+
* @package Mage_Catalog
17+
*/
18+
class Mage_Catalog_Helper_Price extends Mage_Core_Helper_Abstract
19+
{
20+
public const XML_PATH_ROUNDING_PRECISION = 'catalog/price/rounding_precision';
21+
22+
public const ROUNDING_PRECISION_DEFAULT = 2;
23+
24+
public const ROUNDING_PRECISION_MAX = 4;
25+
26+
public const ROUNDING_PRECISION_MIN = 0;
27+
28+
protected $_moduleName = 'Mage_Catalog';
29+
30+
/**
31+
* @SuppressWarnings("PHPMD.StaticAccess")
32+
*/
33+
public function getRoundingPrecision(): int
34+
{
35+
/** @var int<0,4> $precision */
36+
$precision = Mage::getStoreConfigAsInt(self::XML_PATH_ROUNDING_PRECISION);
37+
if ($precision < self::ROUNDING_PRECISION_MIN || $precision > self::ROUNDING_PRECISION_MAX) {
38+
return self::ROUNDING_PRECISION_DEFAULT;
39+
}
40+
41+
return $precision;
42+
}
43+
}

app/code/core/Mage/Catalog/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@
803803
<product>
804804
<default_tax_group>2</default_tax_group>
805805
</product>
806+
<price>
807+
<rounding_precision>2</rounding_precision>
808+
</price>
806809
<product_image>
807810
<base_width>1800</base_width>
808811
<small_width>210</small_width>

app/code/core/Mage/Catalog/etc/system.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@
318318
<show_in_website>0</show_in_website>
319319
<show_in_store>0</show_in_store>
320320
</scope>
321+
<rounding_precision translate="label comment">
322+
<comment><![CDATA[0, 1, 2, 3, 4]]>.</comment>
323+
<label>Catalog Price Rounding Precision</label>
324+
<frontend_type>text</frontend_type>
325+
<sort_order>2</sort_order>
326+
<show_in_default>1</show_in_default>
327+
<show_in_website>0</show_in_website>
328+
<show_in_store>0</show_in_store>
329+
<validate>required-entry validate-number-range number-range-0-4</validate>
330+
</rounding_precision>
321331
</fields>
322332
</price>
323333
<layered_navigation translate="label">

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,10 @@ public function convertPrice($price, $format = false, $includeContainer = true)
10111011
*/
10121012
public function roundPrice($price)
10131013
{
1014-
return round((float) $price, 2);
1014+
/** @var Mage_Catalog_Helper_Price $helper */
1015+
$helper = Mage::helper('catalog/price');
1016+
1017+
return round((float) $price, $helper->getRoundingPrecision());
10151018
}
10161019

10171020
/**

0 commit comments

Comments
 (0)