#39655: Preserve decimal precision for percentage-based custom option prices#40427
#39655: Preserve decimal precision for percentage-based custom option prices#40427sydor-dev wants to merge 2 commits intomagento:2.4-developfrom
Conversation
|
Hi @sydor-dev. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
|
@magento run all tests |
|
Hello, As I can see this issue got fixed in the scope of the internal Jira ticket AC-14050 by the internal team Based on the Jira ticket, the target version is 2.4.9-beta1. Thanks |
Description (*)
This PR fixes precision loss for percentage-based custom option prices when editing products in admin panel.
Problem:
When custom option prices with
price_type="percent"are set via REST API with high precision (e.g., 2.334%), the values are correctly stored in the database with up to 6 decimal places. However, when the product isopened in admin panel and saved (even without changes), these values are truncated to 2 decimal places, causing:
Solution:
PERCENTAGE_PRECISIONconstant (value: 4) inPriceCurrencyInterfaceto support higher precision for percentage pricesAbstractModifier::formatPrice()to accept precision parameter with automatic trailing zero removal while preserving minimum 2 decimal placesCustomOptions::formatPriceByPath()to detectprice_typeand apply appropriate precision:Technical Details:
decimal(20,6)for custom option pricesRelated Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
Scenario: Percentage-based custom option (API → Admin → Save)
Questions or comments
After analyzing the use case, 4 decimal places provide a balanced trade-off between calculation accuracy and admin UI readability. For percentage-based pricing, this level of precision is sufficient even for high-value products. For example, for a product priced at 150,000, a percentage option of 2.3346% results in a price adjustment of 3,501.90, while rounding the percentage to 2.33% would produce 3,495.00—a noticeable difference of 6.90 per item. This demonstrates that precision beyond two decimals is financially significant, while four decimals already capture the meaningful variance in real-world pricing scenarios. At the same time, keeping the UI limited to four decimals avoids unnecessary visual noise compared to exposing the full six-decimal database precision. The database schema remains unchanged and continues to support up to six decimal places, preserving flexibility for future extensions if higher precision becomes necessary.
This change intentionally focuses on Custom Options as the most critical and currently inconsistent case. However, Magento uses percentage-based pricing in several other areas (e.g., Tier Prices, Bundle Options, Sales Rules, Catalog Rules, Shipping Handling), each with different database precision and UI handling. This PR may serve as a foundation for a broader, future effort to standardize percentage precision handling across the platform. A potential next step could be defining a unified strategy or configurable precision policy for percentage values, ensuring consistent behavior between API, database storage, and admin UI while respecting existing schema constraints and backward compatibility.
Backwards compatibility:
This change is fully backwards compatible:
Contribution checklist (*)