0

I am getting

Error filtering template: Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\m3\app\code\Zero\Storelocator\view\frontend\templates\google_reviews.phtml on line 4

line no. 4: $allData = array_slice($allcollection['reviews'], 0, 5, true);

Full code: https://paste.ofcode.org/334bFgXch3bnAWJGaSr5wwh

Function, line no. 3 : getCustomerReview()

Function Code: FUll code : https://paste.ofcode.org/F7Jz4EUa5X9shkZF9MuRu6

function start from line no. 23

Why i am getting this error

Error filtering template: Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\m3\app\code\Zero\Storelocator\view\frontend\templates\google_reviews.phtml on line 4

9
  • Did you try using php's method isset() to check if data exists in $allcollection['review']? Commented Jun 8, 2021 at 7:52
  • Where i can use and how to use it?
    – zus
    Commented Jun 8, 2021 at 7:53
  • You mean line no.4 : paste.ofcode.org/334bFgXch3bnAWJGaSr5wwh
    – zus
    Commented Jun 8, 2021 at 7:54
  • check it out paste.ofcode.org/fkqbXaRsb7Q2FKXcAzj6BZ Commented Jun 8, 2021 at 7:55
  • After add isset() my error : Error filtering template: Notice: Undefined variable: allData in C:\xampp\htdocs\m3\app\code\Zero\Storelocator\view\frontend\templates\google_reviews.phtml on line 14
    – zus
    Commented Jun 8, 2021 at 8:00

2 Answers 2

0

Looks like your curl call returns null sometimes. You can change this line return $allData; in the getCustomerReview method with this

return $allData ?? ['reviews' => []];
1
  • After update : return $allData ?? ['reviews' => []];, i am getting Error filtering template: Notice: Undefined index: reviews in C:\xampp\htdocs\m3\app\code\Zero\Storelocator\view\frontend\templates\google_reviews.phtml on line 4
    – zus
    Commented Jun 8, 2021 at 7:22
0

You need to wrap the code with the isset condition before getting the data. You can use the below code for phtml file:

<?php
$allcollection = $this->getCustomerReview();
if($allcollection && isset($allcollection['reviews'])) {
    $allData = array_slice($allcollection['reviews'], 0, 5, true);
    foreach ($allData as $data) {  
        if (array_key_exists('comment', $data)) {
?> 
            <!-- Star Rating  -->
            <div><?php echo $data['starRating'] ?></div> 

            <!-- Comment  -->
            <div><?php echo $data['comment'] ?></div>

            <!-- Reviewer Name  -->
            <div><?php echo $data['reviewer']['displayName']; ?></div>

            <!-- review Age   -->
            <div><?php echo $data['createTime']; ?></div><br>
        
<?php   } 
    } 
}   
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.