0

I"m trying to retrieve all reviews and ratings for a product. I'm not entirely sure how this could be accomplished. anyone care to help a brutha out?

I am currently trying to use:

            $reviews = Mage::getModel('review/review')
                ->getCollection()
                ->addStoreFilter($storeId)
                ->addStatusFilter(1)
                ->addFieldToFilter('entity_id', 1)
                ->addFieldToFilter('entity_pk_value', $productId)
                ->setDateOrder()
                ->addRateVotes()
                ->getItems();

            $reviewData = array();
            if (count($reviews) > 0) {
                foreach ($reviews as $r) {
                    foreach ($r->getRatingVotes() as $vote) {
                        $ratings[] = $vote->getPercent();
                    }

However, ratings[] is null when i dump the variables.

  • 1
    your traced down that $reviews and $vote are not empty/null? – Stefan Gregori Nov 5 '14 at 15:14
  • @StefanGregori I just checked those variables and noticed that getPercent() did not have parentheses in my production environment (I copied the above from a text editor). It appears the bug was found to be in my very own visual processing Virtual Machine :D Your comment was that which brought this bug to my attention though, thank you! – hownowbrowncow Nov 5 '14 at 15:36
1

If you can also get this data for all products as a collection:

class Amasty_Review_Model_Mysql4_Collection extends  Mage_Review_Model_Mysql4_Review_Product_Collection
{
// approved reviews plus visible in catalog products plus url revrites data
public function addVisiblityFilter()
{
    $store = Mage::app()->getStore();

    $this->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
        ->addStoreFilter($store->getId()) 
        ->addAttributeToSelect(array('name','visibility'), 'inner');

    Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this);   

    $urCondions = array(
        'e.entity_id=ur.product_id',
        'ur.category_id IS NULL',
        'ur.store_id='.intVal($store->getId()),
        'ur.is_system=1'
    );

    $this->getSelect()->joinLeft(
        array('ur' => $this->getTable('core/url_rewrite')),
        join(' AND ', $urCondions),
        array('url' => 'request_path')
    );        

    return $this;
}

public function addRatingData()
{
    $ratingTable = Mage::getSingleton('core/resource')->getTableName('rating/rating_option_vote');
    $this->getSelect()
        ->joinLeft(array('rat' => $ratingTable),
            'rat.review_id = rt.review_id',
            array('av_rating' => new Zend_Db_Expr('AVG(rat.percent)')))
        ->group('rt.review_id');

    return $this;        
}

} 

Your Answer

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

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