Skip to content

str_replace(): Argument #3 ($subject) must be of type array|string, float given error after magento upgrade from 2.3.7 to 2.4.7

I have upgraded magento from 2.3.7 to 2.4.7. And one of the graphql query gives below error

str_replace(): Argument #3 ($subject) must be of type array|string, float given at /vendor/magento/module-catalog-graph-ql/DataProvider/Product/LayeredNavigation/Builder/Price.php:84)

the issue seems fixed when I changed the mentioned line as below:

Original

foreach ($bucket->getValues() as $value) {
            $metrics = $value->getMetrics();
            $result['options'][] = $this->layerFormatter->buildItem(
                isset($metrics['value']) ? str_replace('_', '-', $metrics['value']) : '',
                $metrics['value'],
                $metrics['count']
            );
        }

Changed To

foreach ($bucket->getValues() as $value) {
            $metrics = $value->getMetrics();
            $result['options'][] = $this->layerFormatter->buildItem(
                isset($metrics['value']) && (is_string($metrics['value']) || is_array($metrics['value'])) ? str_replace('_', '-', $metrics['value']) : '',

                $metrics['value'],
                $metrics['count']
            );
        }

But As changing directly to vendor file is not a good practice, How can I correct this.

I am not directly calling this file so I dont see any issue anywhere else.