0

Here is my code (Magento 1.9), but the end result isn't correct. What I would like to achieve is a JSON with all the reviews where I will have User/Customer, review details, rating, sku. So i took products collection, looping trough and pulling reviews if exists, then looping trough reviews creating a list of reviews with all required information. But I messed up something and end result is wrong.

would you please check and let me know what I've done wrong?

<?php
require_once('ack.php');
$date = date_create('now')->format('Ymd');
$obj = new stdClass();
$obj->label = "Product Reviews";
$file = fopen('product_reviews_'.$date.'.csv', 'a+');
$collection = Mage::getModel('catalog/product')->getCollection();
$output = "SKU,Reviews\n";
fwrite($file,$output);
foreach($collection as $product){
    echo '<pre>';
    $productId = $product->getId();
    $reviews = Mage::getModel('review/review')->getCollection()
        ->addStoreFilter(Mage::app()->getStore()->getId())
        ->addEntityFilter('product', $productId)
        ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
        ->setDateOrder()
        ->addRateVotes();
    if($reviews->count() > 0 )  {
        $sku = $product->getSku();
        $reviews_count = $reviews->count();
        $obj->sku = '{' . $sku . '}';
        $obj->reviews = '{' . $reviews_count . '}';
        $output = $sku . ',' . $reviews_count;
        fwrite($file,$output);
        foreach($reviews AS $review) {  
            $votes = $review->getRatingVotes(); 
            $review_detail = $review->detail;   
            $reviewer_name = ucfirst($review->nickname);    
            $reviewer_id = $review['customer_id'];  
            $votes = $review->getRatingVotes(); 
            $total = 0; 
                foreach($votes AS $vote) {          
                    $total += $vote->getPercent();      
                }   
            $avg = $total / count($votes);  
            $rating = $avg/20;
            $output = ','.$review_detail.','.$reviewer_name.','.$rating."\n";   
            $obj->detail .= array( $review_detail );
            $i++;   
            fwrite($file,$output);
        }
        $obj->reviewer = '{' . $reviewer_name . '}';
        $obj->rating = '{' . $rating . '}'; 
    }
}
echo '<pre>';
echo json_encode($obj);

I think I've found problem so ...... thanks everyone for their awesome help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.