'Warning: count(): Parameter must be an array or an object that implements Countable in Magento 2.4.3
protected function _saveAppliedTaxes(
Address $address,
$applied,
$amount,
$baseAmount,
$rate
) {
$previouslyAppliedTaxes = $address->getAppliedTaxes();
$process = count(($previouslyAppliedTaxes));
foreach ($applied as $row) {
if ($row['percent'] == 0) {
continue;
}
Solution 1:[1]
Specified warning is printed when you pass non-array to count(). For example:
<?php
var_dump(count(null));
?>
PHP Warning: count(): Parameter must be an array or an object that implements Countable in /root/a.php on line 4
int(0)
You can suppress warnings by calling error_reporting(0):
<?php
error_reporting(0);
var_dump(count(0));
?>
int(1)
However, it is wiser to check getAppliedTaxes() result and act accordingly.
$previouslyAppliedTaxes = $address->getAppliedTaxes();
$process = is_countable($previouslyAppliedTaxes) ? count($previouslyAppliedTaxes): 0;
One more note, if we check Address model source: https://github.com/magento/magento2/blob/42fe2238acccfd90312c67f2c9ad2702b4a4e6f2/app/code/Magento/Quote/Model/Quote/Address.php#L1241
we can find out that getAppliedTaxes() returns either a result of deserialization or an empty array, and as count([]) emits no warning, seems that errorneous non-countable value was somehow stored to the session previously.
E.g. there's some part in your code that gives wrong value to the setAppliedTaxes(), that might indicate another bug, you should debug there, try dumping the value if it is non-countable and analyse your code to see where can it came from.
function var_dump_str($var)
{
ob_start();
var_dump($var);
return ob_get_clean();
}
...
protected function _saveAppliedTaxes(
...
$previouslyAppliedTaxes = $address->getAppliedTaxes();
if (!is_countable($previouslyAppliedTaxes)) {
error_log("DEBUG: previouslyAppliedTaxes is non-countable: type is ". type($previouslyAppliedTaxes) . ", value is '" . var_dump_str($previouslyAppliedTaxes) . "', and last JSON error was '" . json_last_error() . "'", 0)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
