Opencart后台currency.php第141行报错:非数值值与除零错误求助
Hey there! Let's tackle those repetitive warnings you're hitting in your OpenCart admin's D:\xampp\htdocs\opencart\admin\model\localisation\currency.php file at line 141. These two errors are directly linked, so we can resolve them together with a few targeted checks and fixes.
First, Understand the Root Cause
Line 141 in this file is almost certainly handling currency conversion logic—likely a division operation like:
$converted_value = $original_value / $currency_rate;
The warnings pop up because:
$currency_rateis either a non-numeric value (empty string,null, text, etc.) triggering the "non-numeric value" warning, or$currency_rateis exactly0, causing the "division by zero" error.
Step 1: Check Your Currency Data in the Database
First, let's rule out bad data in your OpenCart database:
- Open your database management tool (like phpMyAdmin for XAMPP).
- Locate the
oc_currencytable (prefix might vary if you changed it during setup). - Look at the
valuecolumn for all currency entries:- If any row has an empty value, non-numeric text, or
0, that's the culprit. - For your store's base currency, this value should always be
1(since it's the reference point for other currencies). For other currencies, it should be a valid positive numeric rate (e.g.,1.05for EUR to USD).
- If any row has an empty value, non-numeric text, or
Step 2: Fix the Immediate Error
Update the problematic value entry in the oc_currency table to a valid numeric rate. For example:
- If it's your base currency, set it to
1. - For other currencies, use the current market exchange rate from a reliable source.
Step 3: Add Code Safeguards to Prevent Future Errors
To stop this from happening again, add validation logic right before line 141 in currency.php:
- Open the file and find line 141.
- Insert this code immediately above the division operation:
// Validate currency rate before division $currency_rate = $this->config->get('currency_value'); if (!is_numeric($currency_rate) || $currency_rate == 0) { // Fallback to a safe default (1 = no conversion) $safe_rate = 1; // Log the issue for debugging (check your server's error logs later) error_log("Invalid currency rate detected: " . $currency_rate); } else { $safe_rate = $currency_rate; }
- Replace the division line to use
$safe_rateinstead of the original variable. For example, if line 141 was:
$value = $value / $currency_rate;
Change it to:
$value = $value / $safe_rate;
Step 4: Prevent Recurrence
- Check auto-update plugins: If you use an automatic currency rate update extension, verify it's configured correctly and pulling valid numeric rates (some plugins might fail silently and store bad data).
- Manual input checks: When editing currency rates in the OpenCart admin, double-check that you're entering only positive numbers (no spaces, letters, or zeros unless it's intentional—and zeros should never be used for active currencies).
- Periodic audits: Every few weeks, spot-check the
oc_currencytable to ensure allvalueentries remain valid.
内容的提问来源于stack exchange,提问作者Syed Ali

