2

I'm manually creating reviews in Magento and I'm trying to find out how I add the rating information in? I can add the reviews no problem but I'm struggling with the rating values (star values). I have an array that looks like this: array("Price"=>80, "Value"=>60, "Quality"=>60);

How can I add that to the star system and the Summary Rating?

Thanks.

Ok, so this is what I have so far: This adds a review:

$review->setEntityPkValue(23);//product id
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId(Mage::app()->getStore()->getId());                    
$review->setStatusId(1); //approved
$review->setNickname("Me");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));                    
$review->save();
$review->aggregate();

This adds a rating for a review <-I'm stuck here!

// this is some test code to add the rating review
$rating[0]['Price']     = 80;
$rating[0]['Value']     = 100;
$rating[0]['Quality']   = 80;
$product_id = 23;
$review_id = 631;
foreach ($rating as $ratingId => $optionId) {
// This is the bit where it all seems to go wrong!:
        Mage::getModel('rating/rating')
        ->setRatingId(1)
        ->setReviewId($review_id)
        ->addOptionVote($val, $product_id);
}

Thanks!

3
  • 1
    It's not clear what you mean by "manual". Are you writing code to create the reviews? And looking for what you need to do to add a rating value? Post the code you have do far and you'll me more likely to get an answer. Dec 2, 2010 at 20:38
  • Hi Alan. Thanks for replying. Yes I'm writing code to create the review and add the rating value (Price, Quality, Value etc). I've written code that creates the review, it just doesn't add the rating. I'm away from my desk now so I can't get to the code I've written but I'll post it tomorrow when I can. Thanks
    – sulman
    Dec 2, 2010 at 21:07
  • Ok I've added my code to the question now. Thanks!
    – sulman
    Dec 3, 2010 at 9:32

1 Answer 1

1

This worked for me:

public function addReview($ratingarray)
{
    $product_id = $ratingarray['product_id'];
    $storeid = $ratingarray['store_id'];
    $title = $ratingarray['title'];
    $customerid = $ratingarray['customer_id'];
    $nickname = $ratingarray['nickname'];
    $detail = $ratingarray['detail'];

    $review = Mage::getModel('review/review');
    $review->setEntityPkValue($product_id);
    $review->setStatusId(1);
    $review->setTitle($title);
    $review->setDetail($detail );
    $review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
    $review->setStoreId($storeid);
    $review->setStatusId(1); //approved
    $review->setCustomerId($customerid);
    $review->setNickname($nickname);
    $review->setReviewId($review->getId());
    $review->setStores(array($storeid));
    $review->save();
    $review->aggregate();
    //return "success";
    $rating_options = $ratingarray['options'];
    /*array(
     array(1,2,3,4),
            array(6,7,8),
            array(11,12)
    );*/

    $row = count($rating_options);
    $rating_id = 1;
    foreach($rating_options as $key1=>$val1)
    {
        foreach($val1 as $key2=>$val2)
        {
            $_rating = Mage::getModel('rating/rating')
            ->setRatingId($key1)
            ->setReviewId($review->getId())
            ->addOptionVote($val2,$product_id );
        }

    }
    return "Success";
}

I am calling this like => $options = array(1=>array(1,2,3,4),2=>array(6,7,8),3=>array(11,12)); $reviewarray = array('customer_id'=>'21','product_id'=>'176','store_id'=>'4','title'=>'Review','nickname'=>'XYZ','detail'=>'Nice Product with Life time warrenty', 'options'=>$options);

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.